8

How do I structure Python code in modules / packages?

 3 years ago
source link: https://www.codesd.com/item/how-do-i-structure-python-code-in-modules-packages.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How do I structure Python code in modules / packages?

advertisements

Assume I have this barebones structure:

project/
  main.py
  providers/
    __init.py__
    acme1.py
    acme2.py
    acme3.py
    acme4.py
    acme5.py
    acme6.py

Assume that main.py contains (partial):

if complexcondition():
  print providers.acme5.get()

Where __init__.py is empty and acme*.py contain (partial):

def get():
  value=complexcalculation()
  return value

How do I change these files to work?

Note: If the answer is "import acme1", "import acme2", and so on in __init__.py, is there a way to accomplish that without listing them all by hand?


This question asked today, Dynamic Loading of Python Modules, should have your answer.

Related Articles

How do you run Python code using Emacs?

I'm trying to run Python code for testing and debugging using Emacs. How should I debug and run code in *.py files ? I tried using the M-x compile commands . Using M-x compile, I get a compilation buffer that crashes (It says Python is compiling, but

How do I save Python code in JSON?

I've got a large python project with several components, that exchange information with JSON files. Actually, this project is our internal tool for analysis and integration testing, and our developers use it either from web-UI, or from a command line

How to translate this Python code into Node.js

I got a very nice answer on here about how to clear a line / delete a line in a file without having to truncate the file or replace the file with a new version of the file, here's the Python code: #!/usr/bin/env python import re,os,sys logfile = sys.

How to use raw python code in a Django model?

In smarty templates you can use raw PHP code in a template by placing it within the "literal" template tag: {literal} echo 'hello world'; {/literal} How can you use raw python code in a Django template?You can't, that's why you have to create cu

How to organize my Python code in several classes?

I was recently told that I should keep my code in separate files; like main.py, engine.py, settings.py and so on. Although this surely does have benefits, like easier management, scalability and others, to me it seems like it has too many drawbacks..

how to update mac python without reinstalling modules

First,sorry for my poor English! I encountered with some troubles during I updated my python. My original mac python 2.7.6 still remain the same. However,after I installed python 2.7.9 as app, python 2.7.9 became my default python. That is, when I ty

how to use the python multi-processing module

I'm using Intel's Pentium(R)Dual-core E5700 @3.00GHz with 2GB ram. I'm trying to learn Python's multiprocessing module. I wrote a simple program that perfoms addition, but its not working; I'm only a getting number of core 2. Is something wrong with

How to have the python code and the markdown in a cell

Can jupyter notebook support inline python code (arthritic calculations, or plot a figure) in markdown cell, or verse visa. Have both python code and markdown in one cell.I don't think that's possible but there is an extension that you could use: htt

How to parallelize my python code

I have a large file as input to my python code and it will produce the corresponding output file. However, it takes too much time and I want to speed it up. Right now, I split the large file into 1000 smaller files. I want to have a small script that

How do I structure the code correctly in Python?

I have a python setup.py file which installs a certain homebrew package, php and other dependencies for my project. I have total of 5 installations which are written in 5 different functions.This is how the code looks now <code> import standard libs

How do I protect Python code?

I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file. If we distribute the .py files or even .pyc files it will be

tensorflow: How to put my python code on Google Cloud as a service

I'm a begginer in TensorFlow and I have some doubts about how to deploy my code in google cloud. I ran de retrain.py in my local machine and I trainned the Inception with my own pictures. I have the graph (.pb and .meta). After that I develop another

How to create a python code for Mac OS X using cx_freeze on Windows?

I have installed cx_freeze and it works like a charm to build .exe but I want to make executable file of my python program for mac os x using windows 7. Can you guide me how I can do it using cx_freeze or any other application. I use following code t

How to convert this Python code to C ++

I have a working algorithm in Python which I want to convert to C++: def gcd(a, b): if (a % b == 0): return b else: return gcd(b, a % b) def solution(N, M): lcm = N * M / gcd(N, M) return lcm / M I'm having a problem with large input values as the mu

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK