I have written the following code and seem to be getting an error, can anyone help? I am using python 3.
Input code:
def fruits(h):
ingredients = fruits.split()
print(ingredients)
print(fruits("apples berries honey"])
Output:
ingredients = fruits.split()
AttributeError: 'function' object has no attribute 'split'
You are calling a method called split() with a function name.
split() function only exists as a method of String type. Thus, you must call and invoke split() with a String type variable not a function.
Maybe, the argument h is a String. So you might want to do h.split()
And you also have the closing bracket, when you call
print(fruits("apples berries honey"])
please modify this code to down below:
print(fruits("apples berries honey"))
Related
I'm trying to render a map that has keys containing minus signs (e.g. first-name). Here is an exemplary template I'm trying to render:
String text = "Dear \"$first-name $lastname\",\nSo nice to meet you";
When I render it with Groovy template engine (can be either SimpleTemplateEngine or MarkupTemplateEngine) it complains and throws an exception. When I remove the '-' from the variable name, it works OK.
Is there anyway to escape these variables in the Groovy template text?
BTW - the reason I'm doing it this way is to render a JSON blob from a 3rd party API.
There is at least one way to do it. It doesn't work in your case for the same reasons you can't use - character in variable name. When you define an expression like:
${first-name}
Groovy sees it as:
${first.minus(name)}
and that is why it throws exception like:
Caught: groovy.lang.MissingPropertyException: No such property: first for class: SimpleTemplateScript1
groovy.lang.MissingPropertyException: No such property: first for class: SimpleTemplateScript1
at SimpleTemplateScript1.run(SimpleTemplateScript1.groovy:1)
You can keep keys like first-name in your bindings, but you would have to put them in a map, so you can access first-name value using Map.get(key) function. Consider following example:
import groovy.text.SimpleTemplateEngine
def engine = new SimpleTemplateEngine()
def bindings = [
'first-name': 'Test',
'lastname': 'qwe'
]
String text = 'Dear "${map["first-name"]} ${map["lastname"]}",\nSo nice to meet you'
println engine.createTemplate(text).make([map: bindings])
In this example I put bindings into a map with a key map. When I do so I can access my bindings as map['first-name'] and map['lastname']. Running this example produces expected output:
Dear "Test qwe",
So nice to meet you
Hope it helps.
I'm trying to register an new Transaction object on the DB using Django, but I'm having TypeError: expected string or bytes-like object when I try to do user_id = user.id I can't really understand why this is happening, since I do the same steps when registering a new Bank object (as shown on prints below). I've tried to debug and the local variables have the correct value, I've also tried to cast user.id with string or int, but none of them worked.
traceback console error create Transaction method create Bank method
models.py
Firstly, please don't post code or errors as images; they are text, they should be posted as text in the question.
However I don't see anything in any of those snippets that suggest the error is with the user - that line is probably highlighted because it's the last in that multi-line call.
Rather, the error looks to be in the reference to date.today - if that's the datetime.date class, then today is a method, which you would need to call:
Transaction.objects.create(date=date.today(), ... )
Or, since that field has a default anyway, you could leave out the date attribute from the create call altogether.
Doing a simple test of a clicking dropdown and seeing if the menu is displayed.
dropdown_user = self.browser.find_element_by_id('dropdown-user')
dropdown_user.click()
expanded = dropdown_user.get_attribute("aria-expanded")
self.assertTrue= (expanded)
settings = self.browser.find_element_by_id('dropdown-user-settings')
self.assertTrue(settings.is_displayed())
Gives me this error when I run the test. I cant figure why settings is a str.
self.assertTrue(settings.is_displayed())
TypeError: 'str' object is not callable
I can't comment (not enough rep) or I would - could you post the whole stack trace? The line self.assertTrue= (expanded) looks like it could feasibly cause an issue.
Edit: I think you're assigning the value of the variable expanded to self.assertTrue, then when you try to call self.assertTrue you're trying to call a string, rather than a function. Remove the line self.assertTrue=(expanded) and replace it with self.assertEqual(expanded, 'true').
Edit 2 to explain in more depth as requested:
The value of expanded is a string - probably 'true', if your dropdown is expanded.
Writing self.assertTrue=(expanded) is the same (in this case) as writing self.assertTrue=expanded. You're assigning the value of the variable expanded (which is a string) to the variable self.assertEqual - it is no longer a function, it's a string!
self.assertTrue(True) # fine
self.assertTrue=('Woops!') # the value of self.assertTrue is now the
# string 'Whoops!'
print(self.assertTrue)
>'Woops!'
self.assertTrue(True) # you're trying to call a string here
> TypeError: 'str' object is not callable
In python, there's nothing to stop you from assigning any type to any variable, because it's dynamically typed.
def runscan(self):
p = os.popen('LD_PRELOAD=/usr/libv4l/v4l1compat.so zbarcam
/dev/video0','r')
while True :
code = p.readline().split(':')[1]
print 'Got barcode:', code
def input(self):
self.entryc.insert(END, code)
how about this? i want use local 'code' to the next function to insert the result of barcode to my Tkinter entryBox.. Thanks
just pass it as a parameter. change the definition of input to def input(self, code) and then as the last line of runscan call input(code). A side note, you shouldn't use "input" as the name of a function, because it shadows the built-in input function, which is used for getting user input from the console.
I am building a survey site in Yesod (0.10) and am getting lost in the types.
Here is a simplified version on what I am trying to do.
invitation url = do
render <- getUrlRender
return $ renderHtml [hamlet|
<p>Dear foo, please take our
<a href=#{ShowSurveyR url}>survey.
|] render
Another function is going to call this in hopes of getting something that can be passed to simpleMail from Network.Mail.Mime. The above function gives a type error:
Handler/Root.hs:404:13:
The function `renderHtml' is applied to two arguments,
but its type `Html -> LT.Text' has only one
This is confusing, because the templates tutorial seems to do things this way.
But if I modify the code, like this...
invitation url = do
return $ renderHtml [hamlet|
<p>Dear foo, please take our
<a href=#{ShowSurveyR url}>survey.
|]
I get this type error.
Handler/Root.hs:403:24:
The lambda expression `\ _render[a4TO] -> ...' has one argument,
but its type `Html' has none
In the first argument of `renderHtml', namely
I think that renderHtml is the wrong function to use, but I can't find what the right function is. Does anyone know what I am missing? How am I supposed to pass the routing function into my hamlet code?
The quasiquote ([hamlet|...|]) returns a function whose argument is also a function. You must first apply that quasiquote value, and pass the results to renderHtml:
[Edit: as #MattoxBeckman discovered, another issue is the need to use getUrlRenderParams instead of gutUrlRender.]
invitation url = do
render <- getUrlRenderParams
return $ renderHtml $ [hamlet|
<p>Dear foo, please take our
<a href=#{ShowSurveyR url}>survey.
|] render
(Note the additional $).
P.S. renderHtml is of type Html -> Text, while the result of the quasiquote, [hamlet|..|], is of type Render url -> Html. The first error message you saw notes that you tried to pass two arguments to renderHtml, while the second error message notes that you didn't pass any arguments to the quasiquote value.
To make this easier for the next person who goes searching for it...
There were two problems. One was pointed out by the first answer; the hamlet quasiquoter itself takes a function. The other problem was that I needed to use the function getUrlRenderParams, not getUrlRender.
The final code is
invitation :: Text -> Handler LT.Text
invitation url = do
render <- getUrlRenderParams
return $ renderHtml $ [hamlet|
<p>Dear foo, please take our
<a href=#{ShowSurveyR url}>survey.
|] render
Just replace shamlet instead of hamlet; it doesn't need a render argument at all.
(As was pointed to me by joelteon at #yesod.)