How to return data correctly

3

I am new programming and I am in the challenge of making an incremental file backup.

I have been based on the paradigm of object-oriented programming and I tried to work using the good practice "DRY" (Do not repeat yourself) to avoid repeating my code.

I tell you how I have distributed my code:

File_main.py classes

import glob
import os


class So:
    pass

class Folder:

    def set_directory(self, directory):
        self.directory = directory
        return directory


class File(Folder, So):

    def __init__(self, directory, extension):
        self.extension = self.set_extension(extension)
        self.directory = self.set_directory(directory)

    def set_extension(self, extension):
        self.extension = extension
        return  extension

    def buscar_archivos(self, directory, extension):
        os.chdir(directory)
        archivos = glob.glob(extension)
        for i in range(len(archivos)):
            filenames = archivos[i]
        return filenames

On the other hand I have an additional file backup_incremental.py

from clases_main import File

directorio = 'D:/'
extension = '*.txt'

# Objeto de Instancia
search_archivos = File(directorio, extension)


archivos = search_archivos.buscar_archivos(directorio, extension)

for i in range(len(archivos)):
    print(archivos)

I would like to know how I can search for the txt files in that directory and display the names when I run the file backup_incremental.py , when I do the for in this file, the names are repeated for some reason, I would also like to start learning to show data in json if you can include me some advice on this I will also thank you immensely.

Edit: I've updated the code to not print as much

Greetings and thanks!

    
asked by Hugo Lesta 02.01.2017 в 23:22
source

2 answers

3

You are printing more in the search_files method:

import glob
import os

class File(object):

    def __init__(self, directory, extension):
        self.extension = extension
        self.directory = directory

    def buscar_archivos(self):
        os.chdir(self.directory)
        return glob.glob(self.extension)                            

And in backup_incremental.py , use set to remove duplicates:

from clases_main import File

directorio = 'D:/'
extension = '*.txt'

# Objeto de Instancia
search_archivos = File(directorio,extension)


archivos = search_archivos.buscar_archivos()

for archivo in set(archivos):
    print(archivo)                                                    
    
answered by 02.01.2017 в 23:36
2

Well, let's start with the DRY.

In python it is not necessary to create getters or setters, so you can omit the methods that start with set_, that is:

...
class Directorio(object):
    def __init__(self, directory, extension):
        self.extension = extension
        self.directory = directory

    def archivos(self):
        directory = self.directory  # obtienes el directorio desde el __init__
        extension = self.extension  # obtienes la extension desde el __init__
        os.chdir(directory)
        archivos = glob.glob(extension)
        return archivos  # retorna la lista creada


# Luego creas la instancia
directorio = 'D:/'
extension = '*.txt'

search_archivos = Directorio(directorio, extension)
archivos = search_archivos.archivos()
for archivo in archivos:
    print(archivo)
...

The result shows you twice on the screen because you were calling twice print()

To show in JSON, you must add an import and the following method to your class:

...
def to_json(self):
    import json
    archivos = self.archivos()
    my_json = {}
    for archivo in archivos:
        _file = archivo.split('/')
        file = _file[len(_file) - 1]
        _file.pop()
        directory = '/'.join(_file)
        if directory in my_json:
            my_json[directory].append(file)
        else:
            if directory == '':
                directory = self.directory
            my_json[directory] = [file]
    return json.dumps(my_json)
...

To traverse the json, (as you converged to string to pass it to json):

...
# Debes pasar el json a diccionario de python
my_json = json.loads(search_archivos.to_json())

for ruta in my_json:
   print("Archivos en '%(ruta)s': %(archivos)s" % {'ruta': ruta, 'archivos': ', '.join(my_json[ruta])})
...
    
answered by 02.01.2017 в 23:29