Getting value of a Numeric Property in kivy - kivy

I am trying to use a NumericProperty but getting Type errors when trying to use it as a value
My code looks like this
from kivy.properties import NumericProperty
from kivy.uix.widget import Widget
class Segment(Widget):
def __init__(self, segments):
super(Segment, self).__init__()
self.segments = NumericPropery(segments)
def build(self):
for i in range(0, self.segments):
# Do something
I get an error :
for i in range(0, self.segments):
TypeError: range() integer end argument expected, got kivy.properties.NumericProperty.
so I tried using self.segments.get() instead, but then I got this error
TypeError: get() takes exactly one argument (0 given)
apperently the get function expects <kivy._event.EventDispatcher> object argument
Any idea how to get around this?

I had a similar problem with this code ...
class GameModel(object):
some_number = NumericProperty(1)
def __init__(self):
self.some_number = 2
... which raised the error:
TypeError: Argument 'obj' has incorrect type (expected kivy._event.EventDispatcher, got GameModel)
I did declare the property at class level though. In my case the problem was that the class itself was not derived from a Kivy Widget class or - as stated in the error message - from an EventDispatcher Object
Deriving from EventDispatcher fixed my problem:
class GameModel(EventDispatcher):
Hope this is helpful for someone else some day ;-)

You have to declare properties at class level.
class Segment(Widget):
segments = NumericProperty()
This will give the correct behaviour. The problem is that properties do their own management of per-instance values and interacting with the eventloop etc.. If you don't declare them at class level, they don't get to do this, so your functions only see the NumericProperty itself (which is your problem).

Related

Functions in Micro-python

i have a question about micro-python that how to make and call functions in micro-python or any other idea related to functions
my code throwing an error that NameError: name 'my_func' isn't defined
import time
from machine import Pin
led = Pin(2, Pin.OUT)
btn = Pin(4, Pin.IN, Pin.PULL_UP)
while True:
if not btn.value():
my_func()
while not btn():
pass
def my_func():
led(not led())
time.sleep_ms(300)
Generaly, what I do following: Imports then Functions and after that- rest of the flow
Slightly modified your code to pass LED object for function
import time
from machine import Pin
def my_func(myLed):
myLed.value(not myLed.value()) # invert boolean value
time.sleep_ms(300)
led = Pin(2, Pin.OUT)
btn = Pin(4, Pin.IN, Pin.PULL_UP)
while True:
if not btn.value():
my_func(led)
while not btn():
pass
you need to import a function before it can be called.

error undefined class TimeOfDay. when trying to use flutter time

when i use TimeOfDay i get the error undefined class TimeOfDay. it was previously working tho.I am trying to get the time,
TimeOfDay theTime = new TimeOfDay.now();
I don't understand why I get the error.
Maybe you made a mistake and modified the class TimeOfDay inside time.dart, sometimes it happens.
Could you check the source code of the class theTimeTimeOfDay ? because it should not exists.
The class time.dart should start with something like this
#immutable
class TimeOfDay {
I'm 99% sure that you named it like this :
#immutable
class theTimeTimeOfDay {
The class is defined in material.dart (it really is defined in a subfile, but it is part of material.dart).
If the class is undefined for you, you will need to import it:
import 'package:flutter/material.dart';
TimeOfDay getCurrentTime() {
return TimeOfDay.now();
}

My code using user_data_dir does not work and it gives an error saying 'self' is not defined. Can someone explain to me why?

The following is a snippet of my code. When I tried running this, the error states that 'self' is not defined. I copied this code online because I don't really know how to use the function user_data_dir. I know that it works (I'm using Windows to write this) with just store = JsonStore('user.json') but I've been reading that using the function user_data_dir would be useful because it is a general function that creates a writeable path for various systems. Would be great if someone could help explain this!
from kivy.storage.jsonstore import JsonStore
from os.path import join
data_dir = getattr(self, 'user_data_dir')
store = JsonStore(join(data_dir,'user.json'))
class Welcome(Screen):
pass
data_dir = getattr(self, 'user_data_dir')
When you copied this line, it was somewhere inside some class's function:
class Some:
def func(self):
data_dir = getattr(self, 'user_data_dir')
Method located inside class in Python receives self as first parameter.
But not every object has user_data_dir attribute: as inclement noticed above it's App objects attribute. You should do something like:
class MyApp(App):
def build(self):
data_dir = getattr(self, 'user_data_dir')
store = JsonStore(join(data_dir,'user.json'))
# ...
Upd:
You can store path to json file inside app class and access to app instance to get this path with App.get_running_app():
class MyApp(App):
#property # see https://www.programiz.com/python-programming/property
def storage(self):
return join(self.user_data_dir, 'user.json')
and later in any place you want:
class SomeClass():
def some_func(self):
print('here\'s our storage:', App.get_running_app().storage)

_repr_latex_ method not working anymore in Jupyter / IPython notebook

I have an Expr class to represent mathematical expressions in which I defined
def _latex(self):
""":return: string LaTex formula"""
(...)
return res
def _repr_latex_(self):
return r'$%s$'%self._latex() #tried several variations of this...
#property
def latex(self):
from IPython.display import Math
return Math(self._latex())
as you can see on http://nbviewer.ipython.org/github/Goulu/Goulib/blob/master/notebook.ipynb at cell [42], latex is correctly rendered when explicitly specified by the property
but fails next cell when called though _repr_latex_ with a UnicodeDecodeError.
e2(e1)._latex() returns '\sin(3x+2)' with no unicode, so what's wrong here ?
Thanks !
well... it was partly my mistake : my Expr class inherits from my Plot class which has a _repr_svg_ method and Jupyter called this one by default instead of _repr_latex_ ...
Ok But:
the error message in Jupyter/IPython doesn't mention this
I still don't know how to select the default _repr_xxx_ method of a class

Can you use #Shared inside a method for Data Tables

Shown below is a sample code.
Assume that "hi" and "hello" are complex objects and have to be evaluated inside the method.
The code below gives me:
Process finished with exit code -1
Expected result is an "Unrolled" explanation of what passed and failed.
Code:
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
#Unroll
class DataTableTest extends Specification {
def "#actual should equal #expected"() {
#Shared def hi = "hi"
#Shared def hello = "hello"
expect:
actual == expected
where:
actual | expected
hi | hi
hi | hello
}
}
I dont think you can even define Shared vars inside a method as their purpose is to be reusable in other methods as they are high-cost calculated variables. Try to set up the shared variables in the class scope.

Resources