reload file in Python interactive session?
I want to load this source code (fib_src.py) in Python3 interactive session:
def fib(x):
if x<7:
return x
return fib(x-1)+fib(x-2)
with
from fib_src import *
to make it easier call the function with only
fib(5)
The problem comes when I modify the source code because I need reload the file with:
from importlib import reload
reload(fib_src)
But if you notice, I never import the whole module. Accordingly, it sends me an error: NameError: name 'fib_src' is not defined
How can I solve it? (I know that importing with "import fib_src" is solved, but that makes me call to the function with fib_src.fib(num) wich is impractical )
See also questions close to this topic
-
AzureML Machine Learning Tasks
Please email me for an easy AzureML task. Regression and Classification is needed in graphics. I will provide you details and pay you right away after we agree on the budget. email: burakkdedeoglu gmail.com
Thanks for your helps. I am looking forward to start.
-
Methods to remove Time Limit exceeded error from my code?
https://www.codechef.com/MARCH21C/problems/COLGLF4
This is the question, for which I have written the solution.
It shows Time Limit Exceeded(TLE) error.
Pls suggest how do I optimise it further.
I have optimised it to some extent. How do I Optimise it further. Pls Help!
Here in my code: e-> egg, h-> choclate
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #include <bits/stdc++.h> using namespace std; // help returns the min cost for n items int help(int n, int e, int h, int& a, int& b, int& c, int *** dp){ if(n==0) return 0; if(dp[h][e][n]!=-1) return dp[h][e][n]; int p1=INT_MAX,p2=INT_MAX,p3=INT_MAX; if(e>=2){ int ans = help(n-1,e-2,h,a,b,c,dp); // dp[h][e-2] = ans; if(ans != INT_MAX) p1=ans + a; } if(h>=3){ int ans = help(n-1,e,h-3,a,b,c,dp); // dp[h-3][e] = ans; if(ans != INT_MAX) p2 = ans + b; } if(e>=1&&h>=1){ int ans = help(n-1, e-1,h-1,a,b,c,dp); // dp[h-1][e-1] = ans; if(ans != INT_MAX) p3= ans + c; } int f = min(p1,min(p2,p3)); dp[h][e][n] = f; return f; } int main() { // your code goes here fastio(); int T; cin>>T; for(int t=0;t<T;t++){ int n,e,h,a,b,c; cin>>n>>e>>h>>a>>b>>c; // scanf("%d",&n); // scanf("%d",&e); // scanf("%d",&h); // scanf("%d",&a); // scanf("%d",&b); // scanf("%d",&c); int*** dp = new int**[h+1]; for(int i=0;i<=h;i++){ dp[i] = new int*[e+1]; for(int j=0;j<=e;j++){ dp[i][j] = new int[n+1]; for(int k=1;k<=n;k++) dp[i][j][k]=-1; } } for(int i=0;i<=h;i++){ for(int j=0;j<=e;j++){ dp[i][j][0]=0; } } int ans = help(n,e,h,a,b,c,dp); if(ans == INT_MAX) cout<<-1<<'\n'; else cout<<ans<<'\n'; // for(int i=0;i<=h;i++){ // for(int j=0;j<=e;j++){ // cout<<dp[i][j]<<" "; // }cout<<endl; // } for(int i=0;i<=h;i++){ for(int j=0;j<=e;j++){ delete [] dp[i][j]; } delete dp[i]; } delete [] dp; } return 0; }
-
Python Custom Config Files for Modules
I have below Directory Structure:
|-- masterFile.py
|-- firstFolder
|-- |-- first.py
|-- |-- configFile
|-- SecondFolder
|-- |-- second.py
|-- |-- configFileI have many folders like (firstFolder, secondFolder...) and all their python files have same type of code only difference is on processing the variables from config file. I have put all the common code in masterFile.py file and the variable processing has been kept in first.py,m second.py files as a function.
What I want to do is: I will run the masterFile.py file and depending on some input/signal I will call the fucntion from first.py, second.py .. file and I want them to be run with their own config file variables.
I am using below code format:
In first.py, second.py
import os import configparser config = configparser.ConfigParser() config.read(os.path.join(os.path.dirname(__file__),'config')) var1 = config['xyz']['abc'] var2 = config['jkl']['mno'] def doSomeMagic(INPUT_VAR): someProcessing_on var1, var2 & INPUT_VAR
Importing the modules in masterFile.py
from firstFolder import first from secondFolder import second initialization_of INPUT_VAR if SIGNAl == 1: first.doSomeMagic(INPUT_VAR)
Depending on some input/signal, I will call doSomeMagic function from first.py, second.py ... files. I want these function to use their config file but Config files are not accessible when I am following this format/structure. < br>
Any help on how to Achieve this and any advice to change these formats/stucture to make more Pythonic way.
I have some folder with name starting with a digit like: 7Yes
from 7Yes import 7yes
How can I import the python file from this directory?
-
How to upgrade Python to latest version in Windows 10?
I'm currently running Python version 3.8.5 on windows 10 and the latest version of python is 3.9.2. How can I upgrade Python to the latest version 3.9.2 . Is it possible through Command Prompt?
-
'psycopg2.extensions.connection' object has no attribute 'get_dsn_parameters'
I've just upgraded postgresql-client from 9.1 to 9.5 on Ubuntu 16.04. (I plan to upgrade everything further, but I want to get this working first.) I have psycopg2 2.6.1 installed, but when I try to execute
print(conn.get_dsn_parameters())
I get the error message
'psycopg2.extensions.connection' object has no attribute 'get_dsn_parameters'
even though the psycopg2 docs say
get_dsn_parameters
requires libpq >= 9.3. From this I infer psycopg2 has linked against libpq 9.1; would this be correct?pip
refuses to uninstall psycopg2, so I can't do uninstall-reinstall. How do I get psycopg2 working with the correct version of libpq?Thanks.
-
How to compare files in two zip file are totally same or not?
I have two zip files. I want to see if everything (file name and each file's contents in the zip) are the same.
There is a similar question. But the answer does not support zip files.
Anyone has a good idea?
-
How can I get all the stocks of each accounts in Interactive Broker IB API?
I have a Master Account which manages multiple accounts. How can I get the portfolio of each accounts? I've been trying the
self.reqPositions()
but it returns just the accounts with recent trades.for example I have 3 accounts and the I have transactions on just 1 account so the 2 other accounts do not have any transactions on that day but I want to get their current portfolio:
U1234567 AAPL - 200 stcks MSFT - 200 stcks U7654321 AAPL - 300 stcks FB - 500 stcks
Follow up question would be, how can I place an order for each account not on Master Account? Thank you for answering.
-
Making Python Enums safe for interactive work in which one may execute the file multiple times
If I'm doing interactive work in Python and have an
Enum
that looks like thisfrom enum import Enum class UnsafeColor(Enum): RED = 1 GREEN = 2 BLUE = 3
I may do
[In]: socks = UnsafeColor.RED <rerun file> [In]: shoes = UnsafeColor.RED [In]: socks == shoes [Out]: False
This is kind of surprising, but the problem is that rerunning the file has created a new
UnsafeColor
class with members that is not equal to the old one. I can avoid this by doing something likefrom functools import lru_cache try: get_interactive_safe_enum except NameError as e: @lru_cache(maxsize=None) def get_interactive_safe_enum(*args, **kwargs): return Enum(*args, **kwargs) SafeColor = get_interactive_safe_enum("SafeColor", "RED GREEN BLUE")
Now the following works as expected:
[In]: hat = SafeColor.RED <rerun file> [In]: shirt = SafeColor.RED [In]: hat == shirt [Out]: True
This works, but it's not winning any awards. There appears to be lots of singleton and metaclass magic going on with
Enum
, so I wonder, is there a better way to accomplish this?