Accessing A Python Module + General Library/Module Structure
Below is a screenshot of part of an article explaining how to access the example Python module dataset.py, for which they provide the following line:
import my_model.training.dataset
I'd like to know if the following methods below are equivalent and accomplish the same thing:
from my_model.training import dataset
from my_model import training.dataset
I have a library where I've been accumulating all of my .py files over time. I'm trying to organize it into something more.. neat but I'm having trouble deciding how to do that.
The library (or rather, the folder I'm dumping everything in) is meant to be just a collection of independent modules, but some of the modules have cross dependencies.. It'd be easier if I had a systematic way to group functions/classes within certain files ie modules. Should they be grouped by purpose?
keep in mind these aren't even packages for projects, they are the building blocks for other packages; just my own personal collection of classes and functions but starting to get hard to manage. so i could use some advice
Thanks
do you know?
how many words do you know
See also questions close to this topic
-
Python File Tagging System does not retrieve nested dictionaries in dictionary
I am building a file tagging system using Python. The idea is simple. Given a directory of files (and files within subdirectories), I want to filter them out using a filter input and tag those files with a word or a phrase.
If I got the following contents in my current directory:
data/ budget.xls world_building_budget.txt a.txt b.exe hello_world.dat world_builder.spec
and I execute the following command in the shell:
py -3 tag_tool.py -filter=world -tag="World-Building Tool"
My output will be:
These files were tagged with "World-Building Tool": data/ world_building_budget.txt hello_world.dat world_builder.spec
My current output isn't exactly like this but basically, I am converting all files and files within subdirectories into a single dictionary like this:
def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree
Right now, my dictionary looks like this:
key:''
.In the following function, I am turning the empty values
''
into empty lists (to hold my tags):def empty_str_to_list(d): for k,v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v)
When I run my entire code, this is my output:
hello_world.dat ['World-Building Tool'] world_builder.spec ['World-Building Tool']
But it does not see
data/world_building_budget.txt
. This is the full dictionary:{'data': {'world_building_budget.txt': []}, 'a.txt': [], 'hello_world.dat': [], 'b.exe': [], 'world_builder.spec': []}
This is my full code:
import os, argparse def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree def empty_str_to_list(d): for k, v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v) parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--filter", action="store", help="keyword to filter files") parser.add_argument("--tag", action="store", help="a tag phrase to attach to a file") parser.add_argument("--get_tagged", action="store", help="retrieve files matching an existing tag") args = parser.parse_args() filter = args.filter tag = args.tag get_tagged = args.get_tagged current_dir = os.getcwd() files_dict = fs_tree_to_dict(current_dir) empty_str_to_list(files_dict) for k, v in files_dict.items(): if filter in k: if v == []: v.append(tag) print(k, v) elif isinstance(v, dict): empty_str_to_list(v) if get_tagged in v: print(k, v)
-
Actaully i am working on a project and in it, it is showing no module name pip_internal plz help me for the same. I am using pycharm(conda interpreter
File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\Scripts\pip.exe\__main__.py", line 4, in <module> File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\__init__.py", line 4, in <module> from pip_internal.utils import _log
I am using pycharm with conda interpreter.
-
Looping the function if the input is not string
I'm new to python (first of all) I have a homework to do a function about checking if an item exists in a dictionary or not.
inventory = {"apple" : 50, "orange" : 50, "pineapple" : 70, "strawberry" : 30} def check_item(): x = input("Enter the fruit's name: ") if not x.isalpha(): print("Error! You need to type the name of the fruit") elif x in inventory: print("Fruit found:", x) print("Inventory available:", inventory[x],"KG") else: print("Fruit not found") check_item()
I want the function to loop again only if the input written is not string. I've tried to type return Under print("Error! You need to type the name of the fruit") but didn't work. Help
-
how do I dissable debian python path/recursion limit
so, as of late, I've been having path length limit and recursion limit issues, so I really need to know how to disable these.
I can't even install modules like discord.py!!!!
-
TypeError: 'float' object cannot be interpreted as an integer on linspace
TypeError Traceback (most recent call last) d:\website\SpeechProcessForMachineLearning-master\SpeechProcessForMachineLearning-master\speech_process.ipynb Cell 15' in <cell line: 1>() -->1 plot_freq(signal, sample_rate) d:\website\SpeechProcessForMachineLearning-master\SpeechProcessForMachineLearning-master\speech_process.ipynb Cell 10' in plot_freq(signal, sample_rate, fft_size) 2 def plot_freq(signal, sample_rate, fft_size=512): 3 xf = np.fft.rfft(signal, fft_size) / fft_size ----> 4 freq = np.linspace(0, sample_rate/2, fft_size/2 + 1) 5 xfp = 20 * np.log10(np.clip(np.abs(xf), 1e-20, 1e100)) 6 plt.figure(figsize=(20, 5)) File <__array_function__ internals>:5, in linspace(*args, **kwargs) File ~\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\core\function_base.py:120, in linspace(start, stop, num, endpoint, retstep, dtype, axis) 23 @array_function_dispatch(_linspace_dispatcher) 24 def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, 25 axis=0): 26 """ 27 Return evenly spaced numbers over a specified interval. 28 (...) 118 119 """ --> 120 num = operator.index(num) 121 if num < 0: 122 raise ValueError("Number of samples, %s, must be non-negative." % num) TypeError: 'float' object cannot be interpreted as an integer
What solution about this problem?
-
IndexError: list index out of range with api
all_currencies = currency_api('latest', 'currencies') # {'eur': 'Euro', 'usd': 'United States dollar', ...} all_currencies.pop('brl') qtd_moedas = len(all_currencies) texto = f'{qtd_moedas} Moedas encontradas\n\n' moedas_importantes = ['usd', 'eur', 'gbp', 'chf', 'jpy', 'rub', 'aud', 'cad', 'ars'] while len(moedas_importantes) != 0: for codigo, moeda in all_currencies.items(): if codigo == moedas_importantes[0]: cotacao, data = currency_api('latest', f'currencies/{codigo}/brl')['brl'], currency_api('latest', f'currencies/{codigo}/brl')['date'] texto += f'{moeda} ({codigo.upper()}) = R$ {cotacao} [{data}]\n' moedas_importantes.remove(codigo) if len(moedas_importantes) == 0: break # WITHOUT THIS LINE, GIVES ERROR
Why am I getting this error? the list actually runs out of elements, but the code only works with the if
-
Python3: Import Module over Package
I'm looking for suggestions to import a module over a package in Python3. I don't have control over the names of the files so changing them is not an option. Additionally, the submodules are external github repositories that I will constantly pull updates from, hence cannot change the contents of feature.py
This is my directory structure:
foo __init__.py bar_1.py bar_2.py submodules s_module_1 foo.py feature.py s_module_2 s_module_3 main.py
Contents of feature.py
... from foo import foo_methods # import from foo.py module ...
Contents of main.py
from foo import * # imports from foo package import sys sys.path.insert(0, 'submodules/s_module_1') from feature import * # results in error
When I run main.py, the error I get is
ImportError: cannot import name 'foo_methods' from 'foo' (foo/__init__.py)
Any suggestions to resolve this would be appreciated!
-
How to return a reactive dataframe from within a shiny module that depends on a button click?
Aim: Return a reactive dataframe object from within the module named "modApplyAssumpServer" Problem: I am getting an endless loop. Even if I wrap everything within the observeevent logic within isolate()
I have included another table in the app code below to indicate a simplified version of the logic that works outside of the module framework but that I can't seem to get to work within the module.
library(shiny) library(dplyr) df_agg_orig <- data.frame(proj_1 = c(2,3)) modGrowthInput <- function(id) { ns <- NS(id) tagList( numericInput(ns("first"),label = "Assumption",value = 100), ) } modGrowthServer <- function(id, btnGrowth) { moduleServer(id, function(input, output, session) { list( first = reactive({input$first}) ) }) } modButtonUI <- function(id,lbl = "Recalculate"){ ns <- NS(id) actionButton(inputId = ns("btn"),label = lbl)#,style = "pill",color = "primary",no_outline = T,size = "xs" } modButtonServer <- function(id){ moduleServer(id, function(input, output, session) { reactive({input$btn}) }) } modApplyAssumpServer <- function(id,btnGrowth, df_agg,case_vals){ moduleServer(id, function(input, output, session) { stopifnot(is.reactive(btnGrowth)) stopifnot(is.reactive(df_agg)) mod_vals <- reactiveVal(df_agg()) observeEvent(btnGrowth(),{ isolate({mod_vals(df_agg() %>% mutate(proj_1 = proj_1*input$first))}) print("Looping problem...") }) mod_vals() }) } #### Test App GrowthInputApp <- function() { ui <- fluidPage( sidebarPanel(modGrowthInput("tst"),modButtonUI("tstGrowth")), mainPanel(fluidRow( splitLayout( DT::DTOutput("no_module"),DT::DTOutput("module_tbl"))))) server <- function(input, output, session) { btnGrowth <- modButtonServer("tstGrowth") case_vals <- modGrowthServer("tst") df_agg <- reactiveValues(df_wide = df_agg_orig) #Outside of module test exhibiting expected/desired behavior (at least if the looping issue would let it do so :) observeEvent(btnGrowth(),{ df_agg$df_wide$proj_1 <- round(df_agg$df_wide*case_vals$first(),2) }) output$no_module <- DT::renderDT({DT::datatable(rownames = F,df_agg$df_wide,caption = "Not Updated Within Module")}) output$module_tbl <- DT::renderDT({DT::datatable(rownames = F,modApplyAssumpServer("tst",btnGrowth = btnGrowth,df_agg = reactive({df_agg_orig})),caption = "Table Returned From Module")} ) } shinyApp(ui, server) } runApp(GrowthInputApp())
-
Module does not provide an export named default - compiled typescript module
I'm developing a node npm module in typescript, and after I compile it to commonjs and try to import it, I get the error:
SyntaxError: The requested module 'woo-swell-migrate' does not provide an export named 'default'
But... it does have a default export. Here is the compiled index.js file:
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const woocommerce_rest_api_1 = __importDefault(require("@woocommerce/woocommerce-rest-api")); const swell_node_1 = __importDefault(require("swell-node")); const path_1 = __importDefault(require("path")); class WooSwell { /** * * @param config - required params for connecting to woo and swell * * @param dirPaths - directory paths to store json files and images in * @param dirPaths.data - directory to store json files in * @param dirPaths.images - directory where wordpress image backup is stored */ constructor(config, dirPaths) { this.swell = swell_node_1.default.init(config.swell.store, config.swell.key); this.woo = new woocommerce_rest_api_1.default({ consumerKey: config.woo.consumerKey, consumerSecret: config.woo.consumerSecret, url: config.woo.url, version: config.woo.version }); this.wooImages = {}; this.paths = { wooImageFiles: dirPaths.images, wooImageJson: path_1.default.resolve(dirPaths.data, 'woo-images.json'), wooProducts: path_1.default.resolve(dirPaths.data, 'woo-products.json'), swellCategories: path_1.default.resolve(dirPaths.data, 'swell-categories.json') }; } /** * gets all records from all pages (or some pages, optionally) of endpoint * * @param endpoint - example: '/products' * * @param options - optional. if not provided, will return all records from all pages with no filters * * @param options.pages - supply a range of pages if not needing all - example: { first: 1, last: 10 } * * @param options.queryOptions - Swell query options, limit, sort, where, etc. See https://swell.store/docs/api/?javascript#querying * * @returns - record array */ async getAllPagesSwell(endpoint, options) { const res = await this.swell.get(endpoint, options === null || options === void 0 ? void 0 : options.queryOptions); let firstPage = (options === null || options === void 0 ? void 0 : options.pages.first) || 1; let lastPage = (options === null || options === void 0 ? void 0 : options.pages.last) || Object.keys(res.pages).length; let records = []; for (let i = firstPage; i <= lastPage; i++) { const res = await this.swell.get(endpoint, Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.queryOptions), { page: i })); records.push(...res.results); } return records; } /** * gets all records from all pages of endpoint * * @param endpoint example: 'products' * * @param options - optional. * * @param options.pages - supply a page range if not loading all pages { start: 10, end: 15 } * * @returns - record array */ async getAllPagesWoo(endpoint, options) { var _a, _b; const res = await this.woo.get(endpoint); const firstPage = ((_a = options === null || options === void 0 ? void 0 : options.pages) === null || _a === void 0 ? void 0 : _a.first) || 1; const lastPage = ((_b = options === null || options === void 0 ? void 0 : options.pages) === null || _b === void 0 ? void 0 : _b.last) || parseInt(res.headers['x-wp-totalpages']); const records = []; for (let i = firstPage; i <= lastPage; i++) { records.push(...(await this.woo.get(endpoint, { page: i })).data); } return records; } } exports.default = WooSwell;
It's there... right at the bottom.
exports.default = WooSwell
. So why am I getting this error?Here is my package.json:
{ "name": "woo-swell-migrate", "version": "1.0.0", "description": "", "main": "./dist/index.js", "types": "./dist/index.d.ts", "type": "module", "scripts": { "build": "tsc", "test": "jest --config jestconfig.json" }, "keywords": [], "license": "ISC", "dependencies": { "@woocommerce/woocommerce-rest-api": "^1.0.1", "dotenv": "^16.0.0", "es2017": "^0.0.0", "image-size": "^1.0.1", "mime-types": "^2.1.35", "swell-node": "^4.0.9", "ts-jest": "^28.0.1" }, "devDependencies": { "@types/mime-types": "^2.1.1", "@types/jest": "^27.5.0", "@types/woocommerce__woocommerce-rest-api": "^1.0.2", "@types/node": "^17.0.31", "jest": "^28.0.3" } }
and my
tsconfig.json
:{ "compilerOptions": { "target": "es2017", "module": "commonjs", "declaration": true, "outDir": "./dist", "esModuleInterop": true, "moduleResolution": "node", "strict": true }, "include": ["src"], "exclude": ["node_modules", "**/__tests__/*"] }
-
NameError within moviepy subclip function
Traceback (most recent call last): File "C:\Users\Evan\Desktop\Gamble\main.py", line 159, in <module> main() File "C:\Users\Evan\Desktop\Gamble\main.py", line 128, in main link = create_clip(count, "WITHDRAW", root) File "C:\Users\Evan\Desktop\Gamble\main.py", line 53, in create_clip new = video.subclip(int(frame / 60 - 5), int(frame / 60 + 5)) File "C:\Users\Evan\anaconda3\envs\F\lib\site-packages\decorator.py", line 232, in fun return caller(func, *(extras + args), **kw) File "C:\Users\Evan\anaconda3\envs\F\lib\site-packages\moviepy\decorators.py", line 79, in wrapper names = inspect.getfullargspec(func).args NameError: name 'func' is not defined
I haven't altered anything in the package nor could I find anything online about this the code causing this error in question is
with VideoFileClip(root) as video: new = video.subclip(int(frame / 60 - 5), int(frame / 60 + 5)) new.write_videofile(f'{root.split(".")[0]}/{claim}{str(datetime.timedelta(seconds=int(frame / 60)))}', audio_codec='aac')
-
Get previous package versions with conda
Given a specific package name when I run
conda list my-package-name
I get its current version. What's a simple way / command to get instead the history of the package versions I've installed in the current environment ?