10
gitpush
6y

Python Question:

I'm learning Python and thought I'd start with web services since that's a concept I'm familiar with. My question is, in the attached code, am I doing it right when it comes to python? Cuz I feel like I'm following the same structure I follow in C# WebAPI and NodeJs

What about naming style? Is is the default for python? cuz I'm also using C# naming style :\

Thanks in advance ranters :)

Comments
  • 3
  • 2
    Oh, one thing. Modules are always lowercase which means the file names should also be lowercase. Same goes for the folders.
  • 3
    @gamingfail123 thank you so much, I will rename those files :D

    By the way if you look at my code, i have in the imports sections: from API.xxxxxx import .....

    When I run the file it says can't find module API

    but if I remove it it works, but VS Code puts a red line under 'from' claiming it cannot find the module, I'm a bit lost here, which is the correct way to import a file from the project?
  • 1
    @gitpush The reason for that is that the import statement normally has it's root in the folder in which your python-file is.

    However you can still go to the parent folder with ..

    That means you could import using

    from ..DataLayer.Models.ServiceInfo import ServiceInfo
  • 2
    @gamingfail123 Btw you can always find the folder Python imports from using

    import sys

    sys.path

    sys.path is the list that holds the paths to import from and you can also change it.
  • 2
    @gamingfail123 Thanks man much appreciated :D

    By the way I was just learning the sys thingy lol, wrote this code:

    os.path.realpath(os.path.join(

    os.getcwd(), os.path.dirname(__file__), 'config.json'))

    Though I'm not sure how it relates to sys, for now I'm using os.path, what is the difference?
  • 2
    @gitpush They are actually completely different. :D

    sys.path is the list of folders where python will look for the things you wanna import.

    os.path is a module that provides you with a lot of utilities for paths.

    https://docs.python.org/3/library/...
  • 2
    @gamingfail123 aaah still have a very long road to go lol

    But I'm happy I am finally going for it, I've been longing to do this since 2012 lol
  • 1
    @gamingfail123 one last question, I'm using JsonPickle, and when I parse mongodb object, the id field is as follows:

    "_id": {

    "py/b64": "WyTrvE+D/csybeGZ\n"

    },

    I am using unpickle=False, though that is the output, how do I make it like this:

    "_id": "WyTrvE+D/csybeGZ\n"
  • 1
    @gitpush I don't really know jsonpickle, but I can look into it if you send me your code snippet. :D

    Btw I'm not sure why you are pickling an ObjectId, I would just use the json module to dump it.
  • 0
    @gamingfail123 Thanks man, I'm using jsonpickle because I was not able to parse classes into json using the default json module.

    My code is as follows:

    Controller Post Method:

    user = jsonpickle.decode(self.request.body)

    repo = UserRepository()

    result = repo.insertUser(user)

    self.set_header("Content-Type", "application/json")

    self.write(jsonpickle.encode(result, unpicklable=False))

    Repository Insert Method:

    db = self.mongoClient[self.dbName]

    db.user.insert_one(user)

    return db.user.find_one({"email": user["email"]})
  • 0
    @gamingfail123 I also made this for tornado so things doesn't get messed up by time, hope I'm doing it right:

    https://pastebin.com/aMJdhJuT

    If you want I can pastebin my earlier code if it is not clear in my comment
  • 1
    @gitpush For this example you can just get the ObjectId as a base64 string and do

    import json

    self.write(json.dumps({"_id": str(object_id)})

    If you want to get more advanced I can definitely recommend diving into custom JSON En/Decoders

    https://docs.python.org/3/library/...
  • 1
    @gamingfail123 thanks man sorry for bothering a lot :)

    I followed your recommendation but ended up doing this:

    userDto.id = str(userDoc['_id'])

    Now I get the id as I want it :D
  • 1
    @gitpush Yeah, I think that's even a little bit better. :D
  • 1
    @gamingfail123 still a noob don't want to rush diving into advanced stuff before I understand the language lol

    Appreciate your help :D
  • 1
    @gitpush You're welcome :D
  • 4
    Just a friendly reminder that semicolons are supported in python.
  • 1
    @ewpratten but when I add them pylint removes them, I'm lost here :/
  • 1
    I cant see your main. But im gonna guide you in the right direction by telling you. Look into the pythonic way of coding python(the only true way of coding in python). This way you will trully learn the language and you'll also be as efficient as possible.
  • 1
    @NillValue I'm trying my best, used it for two days and hoping I reach the point that you said: look into the pythonic way

    I was more of a C# biased in this, still searching and fixing my code until I get the hang of it.

    if you still want to see my main, here is a pastebin: https://pastebin.com/xMJrCgpt
  • 4
    @gitpush really? Huh. It works for me.
  • 1
    @ewpratten maybe you have your own config? I use default ones and I'm using python3.6 not sure if that has something to do with it
  • 4
    @gitpush I just use vim + python3
  • 1
    For naming things, look into pep8 :)
    Oh, you could use pylint which checks this for you, but don't go diehard on pep8 since some rules are a bit overkill
  • 0
    @EngineerCoding will do so thanks man :D

    By the way what is the difference between developing a website using Pythong (django) or for example Vue, React ...etc.

    I'm sensing the only advantage is if I don't want to use a webservice to fetch my data, then those Django, Asp.Net are better
Add Comment