How do i import math into kvlang? - math

how do I import math so that I can assign a labeltext to math.pi?????????
I am only going to provide .kv snippet
#: import math
… some code
Label:
id: Label10
text: str(sin(int(Label2.text) * math.pi/180)
The error:
raise ParserException(self, ln, 'Invalid import syntax')
ParserException: Parser: File "*******************", line 2:
...
1:#: kivy 1.10.1
2:#: import math
3:
4::
...
Invalid import syntax

The syntax is #:import imported_name imported_object, so you could do #:import math math to import the math module with the name math.
This seems trivial in this case, but also extends to things like #:import sin math.sin with the same syntax.

Related

How to import GUN SEA with Deno Fresh?

The first attempt to import GUN from Fresh was to add the gun library from esm to import_map.json, where it correctly works in simple examples of using GUN.
{
"imports": {
...
"gun": "https://esm.sh/gun#0.2020.1237",
}
}
But the problem occurred when I wanted to import additionally gun/sea,
After importing import Sea from "gun/sea";.
I got this error:
error: Uncaught (in promise) Error: Dynamic require of "./lib/text-encoding" is not supported
On GitHub I read to import gun/lib/mobile before importing SEA when such a problem occurs.
But this brings an additional problem:
error: Uncaught (in promise) TypeError: Assignment to constant variable.
I checked the gun/lib/mobile file and it literally contains a few lines of global variables:
import Buffer from "buffer";
import { TextEncoder, TextDecoder } from "text-encoding";
global.Buffer = global.Buffer || Buffer.Buffer;
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
Is there any chance to make SEA work on Fresh?
Add following line inside imports in import_map.json file:
"#gun": "https://esm.sh/gun#0.2020.1237"
Then use this import statement to import GUN in any route/component:
import GUN from "#gun"
Usage (from GUN documentation):
const gun = GUN();
gun.get('mark').put({
name: "Mark",
email: "mark#gun.eco",
});
gun.get('mark').on((data, key) => {
console.log("realtime updates:", data);
});
setInterval(() => { gun.get('mark').get('live').put(Math.random()) }, 9);

I try to use moment.js in vue3(ts),based vite.it can be used,but i can not use 'zh-cn' in my code.thanks

in main.ts
import * as moment from 'moment';
import 'moment/locale/pt';
import 'moment/locale/zh-cn';
console.log(1,moment.locale()); // en
moment.locale('fr');
console.log(2,moment.locale()); // en
moment.locale('zh-CN');
console.log(3,moment.locale()); // pt-BR
console.log(moment.locales())
const app = createApp(App)
app.config.globalProperties.$moment = moment
It can not be changed 'zh-cn',moment.locale() run result is also 'en',use 'moment.locale('zh-CN')' or 'moment.locale('zh-cn')' or 'moment.locale('zh-Cn')' is not useful.
I try to using day.js instead of momentjs.It can be use chinese in my code.
In main.ts.
import dayjs from 'dayjs'
import isLeapYear from 'dayjs/plugin/isLeapYear' // 导入插件
import relativeTime from'dayjs/plugin/relativeTime'
import 'dayjs/locale/zh-cn' // 导入本地化语言
dayjs.extend(relativeTime)
dayjs.extend(isLeapYear) // 使用插件
dayjs.locale('zh-cn') // 使用本地化语言
console.log(dayjs().from(dayjs('1990-01-01')))

How can I use OmegaConf custom interpolation with Hydra

How can I use OmegaConf custom interpolation with Hydra?
Some background:
One can define a custom interpolation for square root:
from omegaconf import OmegaConf
import math
OmegaConf.register_resolver("sqrt", lambda x: math.sqrt(float(x)))
And use it with this config.yaml:
foo: ${sqrt:9}
Loading and printing foo:
cfg = OmegaConf.load('config.yaml')
print(cfg.foo)
Outputs 3.0
When trying this with Hydra:
import hydra
#hydra.main(config_path="config.yaml")
def main(cfg):
print(cfg.foo)
if __name__ == "__main__":
main()
I get the following error:
Unsupported interpolation type sqrt
full_key: foo
reference_type=Optional[Dict[Any, Any]]
object_type=dict
How can I register my resolver when using Hydra?
You can register your custom resolver ahead of time:
config.yaml:
foo: ${sqrt:9}
main.py:
from omegaconf import OmegaConf
import math
import hydra
OmegaConf.register_new_resolver("sqrt", lambda x: math.sqrt(float(x)))
#hydra.main(config_path=".", config_name="config")
def main(cfg):
print(cfg.foo)
if __name__ == "__main__":
main()
This will print 3.0.
This approach will work just as well with the Compose API. The evaluation of the custom resolver is happening when you access the node (lazily).
You just need to register the resolver before you access it.

from numexpr import evaluate on Quantopian

I'm trying to get some technical ind. with some of those commands in this link:https://github.com/enigmampc/catalyst/blob/master/catalyst/pipeline/factors/equity/technical.py,
but in the quant.notebook I'm not able to get "from numexpr import evaluate", so evaluate is not defined.
How can I solve this?
from numexpr import evaluate
class FastochasticOscillator(CustomFactor):
inputs=(USEquityPricing.close,USEquityPricing.high,USEquityPricing.low)
window_safe=True
window_length=14
def compute(self, today, assets, out, closes, highs, lows):
highest_high= nanmax(highs, axis=0)
lowest_low= nanmin(lows, axis=0)
latest_close= closes[-1]
evaluate(
'((tc - ll) / (hh - ll)) * 100',
local_dict={
'tc':latest_close,
'll':lowest_low,
'hh':highest_high,
},
global_dict={},
out=out,
)
K= FastochasticOscillator(window_length=14)
return Pipeline(columns={
'K':K,
},screen=base)
I'm working on the Quantopian notebook and when I attempt to import it gives me this: InputRejected: Importing evaluate from numexpr raised an ImportError. Did you mean to import errstate from numpy?
Actually I do not find a way to import numexpr on Quantopian but on Jupyter it do not give problems. Therefore the problem is related to the online IDE. Moreover, I simply re-write the FastOsc ind. in another way to use it inside the pipeline in the quantopian online IDE.
class Fast(CustomFactor):
inputs=(USEquityPricing.close,USEquityPricing.high,USEquityPricing.low)
window_length=14
def compute(self, today, assets, out, close, high, low):
highest_high= nanmax(high, axis=0)
lowest_low= nanmin(low, axis=0)
latest_close= close[-1]
out[:]= ((latest_close - lowest_low) / (highest_high - lowest_low)*100)

Negative pattern in Pathlib.rglob() function

I need to find all python file in folder excluding __init__.py
My first attempt was
import re
search_path.rglob(re.compile("(?!__init__).*.py"))
Such code fails, so i end up with:
filter(
lambda path: '__init__.py' != path.name and path.name.endswith('.py') and path.is_file(), search_path.rglob("*.py")
)
Looks like rglob does not support python regexps.
Why?
Does rglob supports negative patterns?
Can this code be more elegant?
I need something very much like this too. I came up with this:
import pathlib
search_path = pathlib.Path.cwd() / "test_folder"
for file in filter(lambda item: item.name != "__init__.py", search_path.rglob("./*.py")):
print(f"{file}")
Alternatively, you can use fnmatch.
import pathlib
import fnmatch
search_path = pathlib.Path.cwd() / "test_folder"
for file in search_path.rglob("./*.py"):
if not fnmatch.fnmatch(file.name, "__init__.py"):
print(f"{file}")

Resources