String format argument for traits.Any - python

I'm using enthought canopy's traits to wrap a code for Nipype. I know format argument is %d for integer variables. However, Multiplication variable below should be also a file.
multiplication = traits.Int(argstr='-mul %d', desc='multiplication', position=2)
So, I want to use Any instead of Int but I don't know what the corresponding format argument is for Any. How can I replace %? below?
multiplication = traits.Any(argstr='-mul %?', desc='multiplication', position=2)

I've got the answer multiplication = traits.Any(argstr='-mul %s', desc='multiplication', position=2) It works for both integer and file

Related

Convert string with "_" to int?

I have a function which takes a string input, tries to convert it to integer and then proceeds with two alternative paths depending on whether the conversion succeeded or not:
def make_int(arg):
try:
int_value = int(arg)
except ValueError:
str_value = arg
I now was quite surprised when the string '123_2307_7' was happily converted to the integer 12323077 - whereas I was expecting it to follow the str path here. What details of str -> int conversion is it I have not yet grokked?
As pointed out by #jonrsharpe the docs says you can embed single _ charcaters in your integer literal - which are simply ignored. Closing.
In python you can write any integer/float this way :
XXX_XXX_XXX
Keep in mind that integer/float are objects in python.

How can we understand if the value inside a String is an int or not in Python?

I am writing a code where I am facing the problem and need a solution if it exists.
Suppose we have a following String type variable in Python which contains an integer value.
Eg:x='123'
I know that we can easily convert this by type conversion to int.
However, suppose we have the following list.
x=['123','Spain']
Is there any method in Python by which I can know which element of the list x is Integer contained inside a string and which is purely an Object?
I would recommend this method:
x = "123"
if x.isdigit():
# int
elif x.replace(".","",1).isdigit():
# float
else:
# str
I assume you have similar question with this post.
But, from my perspective, for more general solution (language agnostic), you should learn more about Regular Expression, here also the same question

Python, I can't understand this statement what does that mean

Write a python program to accept 2 "string" numbers for calculation.
Note : convert string number to an Integer before perform the calculation
Any examples answer?
You have to accept input in string, use raw_input(), and you have to parse them in int. And perform your calculation

Iterable error with int when using for / argument ()error while using map. Where am i going worng

I tried bith ways using map nad using for loop but its not working i know for for loop it has to list,tuples or string. So how do i make this work
1
def narcissistic(value):
x = ((value)== sum((c)**len(value) for c in list(value)))
return x
2
def narcissistic(value):
x=(value== (map(lambda c :sum(c**len(value)),value)))
return x
Your issue comes down to confusion about the type of your different objects. Python is a strongly typed language, so each object has a clear type at any given moment and the language generally won't convert anything to another type automatically for you.
Based on the error you're getting, you're calling your function with an int argument. This causes you trouble when you try to call len or iterate on your value. Python ints don't have a length, nor are they iterable, so it's quite understandable that these fail under the circumstances.
What you want to do is create a string representation of your value number. Then you can loop over the characters of the string, and take its len freely.
There's another issue though. You're also trying to do an exponential operation on the c variable in the generator expression. That won't work because c is a string, not a number. It's a one-digit string, but still a str instance! To do math with it, you need to convert it back to a number with int.
Here's a fixed version of your function:
def narcissistic(number):
num_str = str(number)
return sum(int(c)**len(num_str) for c in num_str) == number
I've renamed the very generic value name with number, which should hopefully make it more clear what type each thing is.

How does Python know which number type to use in order to Multiply arbitrary two numbers?

In C, I have to set proper type, such as int, float, long for a simple arithmetic for multiplying two numbers. Otherwise, it will give me an incorrect answer.
But in Python, basically it can automatically give me the correct answer.
I have tried debug a simple 987*456 calculation to see the source code.
I set a break point at that line in PyCharm, but I cannot step into the source code, it just finished right away.
How can I see the source code? Is it possible? Or how does Python do that multiplication?
I mean, how does Python carry out the different of number type in the result of
98*76 or 987654321*123457789, does Python detect some out of range error and try another number type?
I mean, how does Python carry out the different of number type in the result of 98*76 or 987654321*123457789, does Python detect some out of range error and try another number type?
Pretty much. The source code for integer multiplication can be found in intobject.c. It multiplies the integers as C longs, then casts the longs to doubles and multiplies those. If the results are close, the long multiplication didn't overflow. If the results are very different, it switches to Python longs, which use a bignum representation.
The type promotion for mixed arithmetic is:
integer -> long -> float
The narrower type is converted to the wider type, and the multiplication is carried out.
https://docs.python.org/2/library/stdtypes.html#numeric-types-int-float-long-complex
Some examples to see what happens:
987*456 = 450072
987*456L = 450072L
987*456.0 = 450072.0
I hope I understood your question.
Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables.
Python variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable.

Categories