简体   繁体   English

python 通过 mysql 查询的结果作为 class

[英]python pass result of a mysql query as a class

im not a python expert but came across with some weird idea that i dont know if its possible in some way.我不是 python 专家,但遇到了一些奇怪的想法,我不知道它是否可能以某种方式实现。

i have a table with some users like this:我有一张桌子,上面有一些这样的用户:

|user | language|
|1    |    es   |
|2    |    en   |

i have 2 classes with several strings ie我有 2 个带有多个字符串的类,即

 class en():
    h ="hello"
    a ="goodbye"
    
    class es():
    h = "hola"
    a = "adios"

i also have a query that returns en or es for specific user我还有一个为特定用户返回 en 或 es 的查询

flang = q.idioma(uid).replace("'","")// this returns as es or en 

what i want to achieve is... to call:我想要实现的是......打电话:

flang.h but print the string dinamycally flang.h 但打印字符串 dinamycally

for example if user 1 gets flang.h he should see "hola" aswell as as with flang.a "adios" and same thing for user 2 but with strings in english.例如,如果用户 1 获得 flang.h,他应该看到“hola”以及 flang.a“adios”,对于用户 2 也是一样,但字符串是英文的。

nevertheless even when flang gets printed as en or es it doesnt gets recognized as the class is there a way to do that?尽管如此,即使 flang 被打印为 en 或 es,它也不会被识别为 class 有没有办法做到这一点? or could i store all the strings in a database and call them with some weird query as i have said im learning python by myself so i dont know if its possible或者我可以将所有字符串存储在数据库中并使用一些奇怪的查询来调用它们,因为我说过我自己学习 python 所以我不知道它是否可能

By assuming this line q.idioma(uid).replace("'","") will return a string .假设这一行q.idioma(uid).replace("'","")将返回一个string

You can try to put it into dictionary like this你可以尝试像这样把它放入字典

class en():
    h ="hello"
    a ="goodbye"
    
class es():
    h = "hola"
    a = "adios"

languages = {"en": en, "es": es}

flang = languages.get(q.idioma(uid).replace("'",""))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM