Comments (3)
Try this command instead
Without modifying your code, you should be able to run this command:
python a.py B --arg1 arg1 --arg2 arg2 _imp create 3
What was wrong with your command?
Here's what is happening now when you run python a.py B --arg1 arg1 --arg2 arg2 create 3
:
python a.py
: runs the a.py file, where fire.Fire()
is called. Since you pass no arguments to Fire
, it makes everything in a.py
available at the command line. I think you're importing B
in a.py
, so B is available.
B --arg1 arg1 --arg2 arg2
: An instance of B
is constructed with arguments arg1
and arg2
.
create
: Fire then tries to use the create argument to access a member of your instance of B, which fails since B doesn't have a method named create. If instead you access the _imp
member of B, and then call create
, it should do what you're looking for.
from python-fire.
Thanks for explanation. Follow up question. if I initialize another class X and add a method in a.py like this
class A (object):
def __init__(self, arg1, arg2):
self.x = X() <----- added new initialization
self.b = B(arg1, arg2)
self.arg1 = arg1
self.arg2 = arg2
def connect(self) <---- added new method
some implementation code...
x.py
class X(): <-- how can I access or pass the arg1 arg2 .. to X()? So that the methodsin X can access those arguments
def create()
print("x");
if I run the following command
python a.py A --arg1 arg1 --arg2 arg2 connect
connect will have access to all the arguments that were passed to A. Is there any way we could pass these arguments to class X?
from python-fire.
If I understand your question correctly, then one option is to pass them via X
's constructor, just as you do for B
:
class A (object):
def __init__(self, arg1, arg2):
self.x = X(arg1, arg2) # added arg1 and arg2 here
self.b = B(arg1, arg2)
self.arg1 = arg1
self.arg2 = arg2
def connect(self):
some implementation code...
class X():
def __init__(self, arg1, arg2): # methods in X can now access the arguments from A
self.arg1 = arg1
self.arg2 = arg2
def create():
print("x", self.arg1, self.arg2);
Or if you want X
to have even broader access to A
's properties, you could pass an instance of A
itself, like this:
class A (object):
def __init__(self, arg1, arg2):
self.x = X(self) # we give X access to an instance of A
self.b = B(arg1, arg2)
self.arg1 = arg1
self.arg2 = arg2
def connect(self):
some implementation code...
class X():
def __init__(self, a):
self.a = a # X can now access anything from a
def create():
print("x", self.a.arg1, self.a.arg2); # Here's how you'd access arg1 and arg2
from python-fire.
Related Issues (20)
- [feature request] support multiple dialects for boolean parameters HOT 3
- [feature request] Exclude function (kw)args from synopsis, arguments and flags in help output
- guide sample code is entered incorrectly HOT 2
- Version flag alongside other commands HOT 3
- Remove test requirement on mock HOT 4
- Is it possible to pass arg via code and kwargs by cli (sys.argv) ? HOT 1
- cli for function created on the fly HOT 1
- ERROR: Could not consume arg: >> HOT 1
- AttributeError: 'module' object has no attribute 'PY34' HOT 1
- How to set the number of args at least one? HOT 1
- Python 2.7 no longer available in GitHub Actions by default HOT 1
- Unexpected printing (+paging) when using inspect HOT 3
- Cannot parse list of strings containing `is` HOT 5
- Gracias
- unable to install fire HOT 4
- Android 14 HOT 1
- What's the meaning of "available commands: as_interger_ratio | bit_count |..." HOT 2
- How to use the avialable command "S.xx", such as S.count HOT 2
- How to pass '2e9672320848' as a str value? HOT 1
- Optional type args are mistyped in help HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-fire.