Beispiel #1
0
    def getData(self):
        url = 'https://www.nseindia.com/live_market/dynaContent/live_analysis/gainers/niftyGainers1.json'
        req = urllib.request.Request(url,headers={'User-Agent': 'Mozilla/5.0'})
        html = urllib.request.urlopen(req).read().decode('utf-8')
        jsondata = json.loads(html)
        #code to get different color for tiles
        list = []
        tempList=[]
        for i in range(10):
            for j in range(10):
                num=random.randrange(4)
                if tempList.count(num)<3:
                    tempList.append(num)
                    x=num
                    break
            if x == 1:
                list.append('green-tile')
            elif x == 2:
                list.append('red-tile')
            elif x == 3:
                list.append('orange-tile')
            else:
                list.append('purple-tile')

        jsondata['list']=list
        #storing data into rediswork instance
        root=Root()
        root.data=jsondata
        #this thread will get executed after every 5  min
        threading.Timer(300.0, self.getData).start()
        return root
Beispiel #2
0
 def test_return_string(self):
     root2 = Root(redis=FakeStrictRedis, return_object=False)
     val = 11.1
     expected_result = b'11.1'
     root2.part = val
     # flushing dotobject local cache
     root2.flush()
     self.assertEqual(root2.part, expected_result)
def poolRedis(sendSMS, Root, os, logger, retry):
    while True:
        root = Root()
        numbers = root.numbers
        if (numbers == None or numbers == []):
            continue
        for number in numbers:
            try:
                sendSMS(number, logger)
            except:
                logger.error("Sending message failed.")
                retry(number, logger, sendSMS)
        os.system("redis-cli DEL root.numbers")
        root.numbers = []
        sleep(2)
Beispiel #4
0
 def test_child_set(self):
     value = {1, 2, 4}
     expected_result = set(Root.doformat(value))
     self.root.part_set = value
     result = self.red.smembers('root.part_set')
     self.assertEqual(result, expected_result)
     self.root.flush()
     self.assertEqual(self.root.part_set, value)
Beispiel #5
0
 def test_child_nested_iterable(self):
     value = [1, 3, ["a", 3]]
     expected_result = Root.doformat(value)
     self.root.part = value
     result = self.red.lrange("root.part", 0, -1)
     self.assertEqual(result, expected_result)
     self.root.flush()
     self.assertEqual(self.root.part, value)
Beispiel #6
0
 def test_child_dict(self):
     value = {1: 1, 2: 2, 3: 4}
     expected_result = Root.doformat(value)
     self.root.part_dic = value
     result = self.red.hgetall('root.part_dic')
     self.assertEqual(result, expected_result)
     self.root.flush()
     self.assertEqual(self.root.part_dic, value)
Beispiel #7
0
 def test_child_nested_dict(self):
     value = {1: 1, 2: {"a": "hello"}, 3: 4}
     expected_result = Root.doformat(value)
     self.root.part = value
     result = self.red.hgetall('root.part')
     self.assertEqual(result, expected_result)
     self.root.flush()
     self.assertEqual(self.root.part, value)
Beispiel #8
0
 def test_grandchild(self):
     string = "for real?"
     self.root.haha.wahaha = string
     result = self.red.get('root.haha.wahaha')
     expected_result = Root.doformat(string)
     self.assertEqual(result, expected_result)
     self.root.flush()
     self.assertEqual(self.root.haha.wahaha, string)
     self.assertEqual(self.root['haha.wahaha'], string)
Beispiel #9
0
 def test_nested_format(self):
     value = {1: 1, 2: {"a": "hello"}}
     result = Root.doformat(value)
     int_str = bTYPE_IDENTIFIER + b'num' + bITEM_DIVIDER + b'int' + bITEM_DIVIDER
     expected_result = {int_str + b'1': int_str + b'1',
                        int_str + b'2': bTYPE_IDENTIFIER +
                        b'dict' + bITEM_DIVIDER +
                        b'dict' + bITEM_DIVIDER + b'{"a": "hello"}'}
     self.assertEqual(result, expected_result)
Beispiel #10
0
 def __init__(self):
     config = configparser.RawConfigParser(allow_no_value=True)
     config.read("config/database.cfg")
     redis_host = config.get("Redis", "host")
     redis_port = config.get("Redis", "port")
     redis_password = config.get("Redis", "password")
     self.redis_db = redis.StrictRedis(host=redis_host,
                                       port=redis_port,
                                       password=redis_password,
                                       db=0)
     self.root = Root(self.redis_db)
Beispiel #11
0
    def insert(self, tablename, columns, values):
        root = Root()
        if values[1] == None:
            values[1] = "None"
        if values[2] == None:
            values[1] = "No date"

        root[values[0]] = {
            "etag": values[1],
            "date": values[2],
            "url": values[0]
        }
Beispiel #12
0
    def test_numbers(self):
        today = datetime.date.today()
        now = datetime.datetime.utcnow()
        items = (10, 10.1, Decimal("10"), 10+1j, today, now)

        for val in items:
            self.root.part = val
            result = self.red.get('root.part')
            expected_result = Root.doformat(val)
            self.assertEqual(result, expected_result)
            # flushing dotobject local cache
            self.root.flush()
            self.assertEqual(self.root.part, val)
Beispiel #13
0
    def on_get(self, req, resp):
        resp.status = falcon.HTTP_200
        # get covariates to block on; extend and rename as needed
        cov1 = int(req.params['cov1'])
        cov2 = int(req.params['cov2'])
        covs = [cov1, cov2]

        # get existing covariate profile for observations already assigned
        # treatment from redis
        conn = Redis(host=host, port=port, password=pw)
        root = Root(conn=conn)
        dataID = 'data'  # name of redis list where profile is stored; rename as needed
        data = root[dataID]

        # assign treatment
        ntr = 4  # number of treatment groups; change as needed
        tr, data = exact_blocking(covs, ntr, data)  # replace if needed

        root[dataID] = data  # save new covariate profile to redis
        resp.body = 'Treatment=' + str(tr)  # return treatment group
    def _init_redis_(self, use_redis):
        """Initialize redis variables and connections

        - even if use_redis is True, the first_contact_max_sec test is still used
        ::WINDOWS NOTE::
        the first access to redis has about 1 second time penalty on windows platform. idiocracy
        """
        self.use_redis = use_redis
        self.first_contact_max_sec = 2.5  # should be at most 300 ms to be better than hdd sql database
        self.redis_host = 'localhost'
        self.redis_port = 6379
        self.r = None
        if not self.use_redis:
            return

        g_start()
        redis_db = redis.StrictRedis(host="localhost", port=6379, db=0)
        g_end('redis strictRedis initialized')
        g_start()
        redis_db.set('redis', 'running')  # takes ~ 1000 ms on windows
        first_contact = g_end('first redis access')
        g_start()
        redis_running = redis_db.get(
            'redis') == 'running'  # takes ~ 0.1 ms on windows
        g_end('second redis access', 'd')
        redis_running = redis_db.info()['loading'] == 0

        if first_contact > self.first_contact_max_sec:
            # we don't want the redis to actually slow things down on Windows
            redis_running = False

        if redis_running:
            g_start()
            self.r = Root(host="localhost", port=6379, db=0)
            g_end('redisworks root initialized')
            prinf('Redis works root server running.')
Beispiel #15
0
#!/usr/bin/python3
from redisworks import Root
from time import sleep, time
import serial

device_baudrate = 921600
device_port = "/dev/ttyUSB0"
device_timeout = 0.1
buffer_size = 663  #fs = 3315Hz, then 663 samples every 200ms

buffer_data_ch1 = [0] * buffer_size
buffer_data_ch2 = [0] * buffer_size

root = Root()
device = serial.Serial(port=device_port,
                       baudrate=device_baudrate,
                       timeout=device_timeout)
maxADC = 4096
maxVin = 3.3
adcFactor = maxVin / maxADC

while True:
    try:
        i = 0
        sleep(.05)
        for k in range(buffer_size):
            data_raw = device.readline()
            if len(data_raw) == 7:
                data_ch1 = int(data_raw[:3], 16) * adcFactor
                data_ch2 = int(data_raw[3:-1], 16) * adcFactor
                buffer_data_ch1[k] = data_ch1
Beispiel #16
0
 def fetch_latest_by(self, tablename, where, orderBy, url):
     root = Root()
     result = root[url]
     print(result)
     return result
Beispiel #17
0
from werkzeug.utils import secure_filename
from app import app
import xlrd
import json
from redisworks import Root
import configparser
from .dataRedis2 import DataRedis2
from . import mylogging

UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                             'Uploads')
INI = os.path.join(UPLOAD_FOLDER, 'test.ini')
config = configparser.ConfigParser()
config.read(INI)

root = Root(host=config['DEFAULT']['redis_ip'])


def readExcelToRedis():
    for section in config.sections():
        excelItem = os.path.join(UPLOAD_FOLDER, config[section]['file'])
        redisKey = config[section]['redis_key']
        allPagesDict = {}
        book = xlrd.open_workbook(excelItem)
        sheetNames = book.sheet_names()
        # allPagesDict['keys'] = sheetNames
        DataRedis2.root['keys'] = sheetNames
        for sheetName in sheetNames:
            sh = book.sheet_by_name(sheetName)
            num_rows = sh.nrows
            num_cols = sh.ncols
Beispiel #18
0
import redis
from redisworks import Root

my_redis = Root() # redis.Redis('localhost')

data = {"user":"******", }

def check_for_user(user="******"):
    f = my_redis.user
    if f:
        print("user exists", f)
        return True
    else:
        print(f"user {user} does not exist")
        return False

def add_user(data):
    if not check_for_user(data["user"]):
        my_redis. = data

#conn.hmset("pythonDict", {"Location": "Ahmedabad", "Company": ["A/C Prism", "ECW", "Musikaar"]})

my_redis.KL=({'tokens':[1,2,3]})

data = {"user" : "KL", "tokens" : "first_token", "password":""}
add_user(data)




Beispiel #19
0
class RedisworksTestCase(unittest.TestCase):

    """RedisWorks Tests."""
    def setUp(self):
        self.root = Root(redis=FakeStrictRedis)
        self.red = FakeStrictRedis()

    def tearDown(self):
        # Clear data in fakeredis.
        self.red.flushall()

    def test_nested_format(self):
        value = {1: 1, 2: {"a": "hello"}}
        result = Root.doformat(value)
        int_str = bTYPE_IDENTIFIER + b'num' + bITEM_DIVIDER + b'int' + bITEM_DIVIDER
        expected_result = {int_str + b'1': int_str + b'1',
                           int_str + b'2': bTYPE_IDENTIFIER +
                           b'dict' + bITEM_DIVIDER +
                           b'dict' + bITEM_DIVIDER + b'{"a": "hello"}'}
        self.assertEqual(result, expected_result)

    def test_numbers(self):
        today = datetime.date.today()
        now = datetime.datetime.utcnow()
        items = (10, 10.1, Decimal("10"), 10+1j, today, now)

        for val in items:
            self.root.part = val
            result = self.red.get('root.part')
            expected_result = Root.doformat(val)
            self.assertEqual(result, expected_result)
            # flushing dotobject local cache
            self.root.flush()
            self.assertEqual(self.root.part, val)

    def test_return_string(self):
        root2 = Root(redis=FakeStrictRedis, return_object=False)
        val = 11.1
        expected_result = b'11.1'
        root2.part = val
        # flushing dotobject local cache
        root2.flush()
        self.assertEqual(root2.part, expected_result)

    def test_grandchild(self):
        string = "for real?"
        self.root.haha.wahaha = string
        result = self.red.get('root.haha.wahaha')
        expected_result = Root.doformat(string)
        self.assertEqual(result, expected_result)
        self.root.flush()
        self.assertEqual(self.root.haha.wahaha, string)
        self.assertEqual(self.root['haha.wahaha'], string)

    def test_child_set(self):
        value = {1, 2, 4}
        expected_result = set(Root.doformat(value))
        self.root.part_set = value
        result = self.red.smembers('root.part_set')
        self.assertEqual(result, expected_result)
        self.root.flush()
        self.assertEqual(self.root.part_set, value)

    def test_child_dict(self):
        value = {1: 1, 2: 2, 3: 4}
        expected_result = Root.doformat(value)
        self.root.part_dic = value
        result = self.red.hgetall('root.part_dic')
        self.assertEqual(result, expected_result)
        self.root.flush()
        self.assertEqual(self.root.part_dic, value)

    def test_child_nested_dict(self):
        value = {1: 1, 2: {"a": "hello"}, 3: 4}
        expected_result = Root.doformat(value)
        self.root.part = value
        result = self.red.hgetall('root.part')
        self.assertEqual(result, expected_result)
        self.root.flush()
        self.assertEqual(self.root.part, value)

    def test_child_iterable(self):
        value = [1, 3, "a"]
        expected_result = Root.doformat(value)
        self.root.part = value
        result = self.red.lrange("root.part", 0, -1)
        self.assertEqual(result, expected_result)
        self.root.flush()
        self.assertEqual(self.root.part, value)

    def test_child_nested_iterable(self):
        value = [1, 3, ["a", 3]]
        expected_result = Root.doformat(value)
        self.root.part = value
        result = self.red.lrange("root.part", 0, -1)
        self.assertEqual(result, expected_result)
        self.root.flush()
        self.assertEqual(self.root.part, value)

    def test_many_different_children_types(self):
        set_value = {1, 2, 4}
        self.root.part_set = set_value
        dict_value = {1: 1, 2: {"a": 1}}
        self.root.part_dict = dict_value
        list_value = [1, ["b", 3]]
        self.root.part_list = list_value
        self.root.flush()
        self.assertEqual(self.root.part_set, set_value)
        self.assertEqual(self.root.part_dict, dict_value)
        self.assertEqual(self.root.part_list, list_value)

    def test_many_different_children_types2(self):
        mylist = [1, 3, 4]
        self.root.my.list = mylist
        some_date = datetime.datetime(2016, 8, 22, 10, 3, 19)
        self.root.time = some_date
        mydict = {1: 1, "a": "b"}
        self.root.the.mapping.example = mydict
        self.root.flush()
        self.assertEqual(self.root.my.list, mylist)
        self.assertEqual(self.root.time, some_date)
        self.assertEqual(self.root.the.mapping.example, mydict)

    def test_change_key_type(self):
        mylist = [1, 3, 4]
        self.root.something = mylist
        st = "string"
        self.root.something = st
        self.assertEqual(self.root.something, st)

    def test_number_comparison(self):
        self.root.num = 10
        num = self.root.num
        self.assertIsInstance(num, LazyDot)
        self.assertTrue(num > 8)
        self.assertTrue(num < 11)
        self.assertTrue(num <= 12)
        self.assertTrue(num >= 10)
        self.assertFalse(num > 10)

    def test_number_math(self):
        self.root.num = 10
        num = self.root.num
        self.assertIsInstance(num, LazyDot)
        self.assertEqual(num * 2, 20)

    def test_saving_set_removed_the_old_one(self):
        self.root.myset = {1, 2, 3}
        self.root.myset = {4, 5}
        self.root.flush()
        self.assertEqual(self.root.myset, {4, 5})

    def test_saving_list_removed_the_old_one(self):
        self.root.myset = [1, 2, 3]
        self.root.myset = [4, 5]
        self.root.flush()
        self.assertEqual(self.root.myset, [4, 5])
Beispiel #20
0
class Suggest(Resource):
    root = Root(db=1)

    pct = set(punctuation)  # для работы функции punto_switcher
    _en = "qwertyuiop[]asdfghjkl;'zxcvbnm,."
    _ru = "йцукенгшщзхъфывапролджэячсмитьбю"
    pswr = {en_let: ru_let for en_let, ru_let in zip(_en, _ru)}

    FORMAT = '%(asctime)s %(message)s'
    logging.basicConfig(format=FORMAT)
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)

    valid_chars = set(_ru + whitespace +
                      digits)  # для проверки корректности запроса

    def __init__(self):
        parser = reqparse.RequestParser()
        parser.add_argument('count', type=int)
        parser.add_argument('phrase', type=str)
        self.phrase: str = parser.parse_args()['phrase']
        self.phrase: str = self.punto_switcher(
            self.phrase.lower()) if self.phrase is not None else self.phrase
        self.count: int = parser.parse_args()['count'] or 10
        if self.phrase:
            self.logger.debug(self.phrase)
            self.words: List[str] = self.phrase.split()
            self.search_word: str = self.words[-1]
            self.first_let: str = self.search_word[0]
            self.len_word: int = min(len(self.search_word), 9)
            self.start_phrase: str = ' '.join(
                self.words[:-1]) + ' ' if self.words[:-1] else ''
            self.properties: Set[str] = set()
            self.gm_names: Set[str] = set()
        else:
            self.search_word: str = ''

        self.res_list: List[str] = list()
        self.response: Dict[str:(str, list, int)] = dict()

    def get(self):
        if self.is_valid():
            self.create_properties_list()
            if not self.search_word.isdigit():
                self.search_with_properties_list(
                ) if self.properties else self.search_without_properties_list(
                )
            else:
                self.res_list.append(
                    (self.start_phrase + self.search_word, 100, self.gm_names))
            self.add_properties_to_response()
            self.sort_answer()
            self.good_response()
            status = 200
        else:
            self.bad_response()
            status = 400

        return self.response, status

    def delete(self):
        if self.is_valid():
            self.delete_phrase()
            status = 200
        else:
            self.bad_response()
            status = 400

        return self.response, status

    def is_valid(self):
        """
        проверяет валидность запроса
        :return: True если валидный, False если нет
        """
        return True if self.search_word and not [let for let in self.phrase if let not in Suggest.valid_chars] \
            else False

    def delete_phrase(self):
        """
        удаляет слово из словарей "search_words_db" и "suggest_db",
        добавляет данные слова в словарь стоп-слов
        """
        br_dict = Suggest.root.search_words_db[self.first_let]
        try:
            br_dict.pop(self.phrase)
            Suggest.root.search_words_db[self.first_let] = br_dict
            res = 'CORRECT'
        except KeyError:
            res = 'NOT CORRECT'
        else:
            Suggest.root.stop_words |= {self.phrase}
        self.response['search_words'] = res

        br_dict = Suggest.root.suggest_db[self.first_let]
        for ix in range(1, 10):
            str_ix = str(ix)
            try:
                br_dict[str_ix][self.phrase[:ix]].remove(self.phrase)
                if not br_dict[str_ix][self.phrase[:ix]]:
                    br_dict[str_ix].pop(self.phrase[:ix])
                res = 'CORRECT'
            except (ValueError, KeyError):
                res = 'NOT CORRECT'
            self.response[f'suggest_db_{ix}'] = res

        Suggest.root.suggest_db[self.first_let] = br_dict

    def create_properties_list(self):
        """
        Метод ограничивает область поиска, в случае если было введено более одного слова.
        """
        for ix, word in enumerate(self.words[:-1]):
            if word.isdigit():
                continue
            tokens_dict = Suggest.root.search_words_db[word[0]]
            if word not in self.properties:
                word, status = self.find_token_in_properties(word, ix)
                if not status:
                    word, status = self.find_token_in_tokens_dict(
                        tokens_dict, word, ix)
                    if not status:
                        continue
            word_prop = tokens_dict.get(word, False)
            if not word_prop:
                continue
            elif self.properties:
                self.properties.intersection_update(word_prop['properties'])
                self.gm_names.intersection_update(word_prop['gm_name'])
            else:
                self.properties.update(word_prop['properties'])
                self.gm_names.update(word_prop['gm_name'])

    def find_token_in_properties(self, word: str, ix: int):
        """
        производит поиск слова среди списка связанных слов
        :param word: поисковое слово
        :param ix: индекс позиции слова в запросе
        :return: найденое слово в списке и статус True,
        либо, если не найдено, возвращает слово обратно и статус False
        """
        res = None
        while not res:
            token = process.extractOne(word, self.properties, score_cutoff=70)
            if not token:
                res = (word, False)
            elif token[0] not in Suggest.root.stop_words:
                self.words[ix] = token[0]
                self.start_phrase = ' '.join(self.words[:-1]) + ' '
                res = (token[0], True)
            else:
                self.properties.remove(token[0])

        return res

    def find_token_in_tokens_dict(self, tokens_dict: dict, word: str, ix: int):
        """
        Производит поиск слова в общем списке всех слов хранящихся в саджесте
        :param tokens_dict: словарь слов в котором проводится поиск
        :param word: слово для поиска
        :param ix: индекс позиции слова в запросе
        :return: найденое слово в списке и статус True,
        либо, если не найдено, возвращает слово обратно и статус False
        """
        res = ()
        token = [word, 100] if word in tokens_dict \
            else process.extractOne(word, list(tokens_dict.keys()), score_cutoff=70)
        if token:
            new_token = tokens_dict[token[0]]['phrase']
            if new_token in self.properties or (not self.properties
                                                and new_token):
                self.words[ix] = new_token
                self.start_phrase = ' '.join(self.words[:-1]) + ' '
                res = (token[0], True)
        return res if res else (word, False)

    def search_with_properties_list(self):
        """
        Поиск среди ограниченого списка связанных слов
        """
        tokens = self.search_token(self.search_word, list(self.properties), 70,
                                   self.count)

        if not tokens:
            try:
                search_list = Suggest.root.search_words_db[self.first_let]
                search_word = search_list[process.extractOne(
                    self.search_word, search_list.keys())[0]]['phrase']
                tokens = self.search_token(search_word, list(self.properties),
                                           70, self.count)

            except Exception as ex:
                self.logger.error(ex)
        self.sort_gm_names()
        self.res_list = [[
            self.start_phrase + token, percent,
            tuple(self.gm_names)
        ] for token, percent in tokens if token not in self.root.stop_words]

    def search_without_properties_list(self):
        """
        Первоначальный поиск не использующий список связанных слов
        """
        search_list = Suggest.root.suggest_db[self.first_let][str(
            self.len_word)]
        tokens = self.search_token(self.search_word, list(search_list.keys()),
                                   50, 3)

        for token, percent in tokens:
            tokens_list = search_list[token[:self.len_word]]
            token = process.extractBests(self.search_word,
                                         tokens_list,
                                         limit=3,
                                         score_cutoff=50)
            self.logger.debug(msg=[token, tokens_list])

            for word, percent in token:
                if word not in self.root.stop_words:
                    word_prop = process.extractOne(
                        word, Suggest.root.search_words_db[word[0]].keys())[0]
                    gm_names = Suggest.root.search_words_db[
                        word[0]][word_prop]['gm_name']

                    self.res_list.append(
                        (self.start_phrase + word, percent, gm_names))

    def search_token(self, search_word: str, search_list: list, percent: int,
                     limit: int):
        """
        метод для поиска токена по заданном списку
        :param search_word: токен
        :param search_list: список в котором производится поиск
        :param percent: минимальный процент "похожести"
        :param limit: максимальное число токенов
        :return: список токенов формата [[токен, процент], ...]
        """
        if search_word in self.root.stop_words and search_word in search_list:
            search_list.remove(search_word)

        if search_word in search_list:
            tokens = [[search_word, 100]]
        else:
            tokens = process.extractBests(search_word,
                                          search_list,
                                          score_cutoff=percent,
                                          limit=limit)
        return tokens

    def good_response(self):
        """
        Составляет валидный ответ клиенту
        """
        self.response['response'] = [{
            "suggest": word,
            "gm_name": tuple(gm)[:5],
            "rating": percent
        } for word, percent, gm in self.res_list[:self.count]]
        self.logger.debug(self.response)

    def bad_response(self):
        """
        В случае не валидного запроса - указывает на ошибку которую допустил клиент
        """
        if self.phrase is None:
            message = 'Аргумент `phrase` не был передан в запросе.'
        elif not self.phrase:
            message = 'Аргумент `phrase` пуст.'
        else:
            message = 'Аргумент `phrase` содержит недопустимые символы.'
        self.logger.debug(message)
        self.response['response'] = message

    def sort_answer(self):
        """
        Сортирует ответы по соответствию запросу
        """
        gm_dict = {
            self.del_dupl_words(self.normalize_words(word)): gm_name
            for word, p, gm_name in self.res_list[1:]
        }
        res_list = process.extractBests(self.phrase,
                                        gm_dict.keys(),
                                        limit=self.count)
        self.res_list = self.res_list[:1] + [(word, percent, gm_dict[word])
                                             for word, percent in res_list]

    def sort_gm_names(self):
        """
        Сортирует родовые товары по вероятности соответствия запросу
        """
        self.gm_names = [
            gm_name[0] for gm_name in process.extractBests(
                self.phrase, self.gm_names, limit=5, score_cutoff=50)
        ]

    def add_properties_to_response(self):
        """
        Если ответов меньше заданного числа(self.count), добавляет возможные варианты уточнения
        """
        for phrase, percent, gm_name in self.res_list:
            word = phrase.split()[-1]
            if not word.isdigit():
                properties = set(
                    Suggest.root.search_words_db[self.first_let].get(
                        word, dict()).get('properties', set()))
            else:
                properties = set()
            properties = list(properties & self.properties) if self.properties and properties \
                else list(properties | self.properties)

            [
                self.res_list.append((f'{phrase} {prop}', percent, gm_name))
                for prop in properties[:10 - len(self.res_list)]
                if prop not in Suggest.root.stop_words
            ]

            if len(self.res_list) >= self.count:
                self.res_list = self.res_list[:self.count]
                return

    @staticmethod
    def normalize_words(phrase: str):
        """
        Если в ответе есть англоязычные слова - то переводит их на латиницу
        """
        phrase = phrase.split()
        for ix, word in enumerate(phrase):
            try:
                word = Suggest.root.search_words_db[word[0]][word]['phrase']
                phrase[ix] = word
            except KeyError:
                continue
        return ' '.join(phrase)

    @staticmethod
    def del_dupl_words(phrase: str):
        """
        Удаляет дупликаты слов в строке
        :param phrase: строка
        :return: строка без повторяющихся слов
        """
        without_dupl = list()
        [
            without_dupl.append(word) for word in phrase.split()
            if word not in without_dupl
        ]
        return ' '.join(without_dupl)

    @staticmethod
    def punto_switcher(word: str):
        """
        Переводит англоязычные слова на русскую раскладку и удаляет знаки препинания
        """
        word = [Suggest.pswr.get(let, let) for let in word]
        return ''.join(let for let in word if let not in Suggest.pct)
Beispiel #21
0
from redisworks import Root
import datetime

root = Root()

# ----- Saving to Redis -----

root.my.list = [1, 3, 4]
some_date = datetime.datetime(2016, 8, 22, 10, 3, 19)
root.time = some_date
root.the.mapping.example = {1: 1, "a": "b"}
root['blah'] = 'foo'
root['blah.ding'] = 'foo too'

# ----- Flushing local cache -----
root.flush()

# ----- Loading from Redis -----
thetime = root.time
thelist = root.my.list
mydict = root.the.mapping.example
mydict_again = root['the.mapping.example']
blah = root.blah
blah_ding = root.blah.ding

print(thetime)
print(mydict)
print(mydict_again)
print(thelist)
print(blah)
print(blah_ding)
Beispiel #22
0
class DataRedis2():
    root = Root(host='localhost')

    def __init__(self, dataName, _item):

        self.dataName = dataName
        self.redisFormat = "{}.{}".format(dataName, _item[0])
        self.content = _item
        # print(type(self.content))
        # self.__dict__.update(_item)

    def save(self):
        lock.acquire()
        list = DataRedis2.root[self.dataName]['contents']
        list_name = [it[0] for it in list]
        # print(list)
        if (self.content[0] not in list_name):
            logging.info("adding {}...".format(self.content[0]))
            list.append(self.content)
            lock.release()
        else:
            lock.release()
            logging.error(
                f"This Objects {self.dataName} {self.content[0]} alrerady exists"
            )
            raise (
                f"This Objects {self.dataName} {self.content[0]} alrerady exists"
            )
        post = {
            'heads': DataRedis2.root[self.dataName]['heads'],
            'contents': list
        }
        DataRedis2.root[self.dataName] = post

    def update(self, oldData):
        lock.acquire()
        list = DataRedis2.root[self.dataName]['contents']
        if (oldData in list):
            logging.info("updating {}...".format(self.content[0]))
            index = list.index(oldData)
            list[index] = self.content
            post = {
                'heads': DataRedis2.root[self.dataName]['heads'],
                'contents': list
            }
            DataRedis2.root[self.dataName] = post
            lock.release()
        else:
            lock.release()
            logging.error(
                f"This Objects {self.dataName} {oldData[0]} has been updated or deleted by others"
            )
            raise (
                f"This Objects {self.dataName} {oldData[0]} has been updated or deleted by others"
            )

    def delete(self):
        lock.acquire()
        list = DataRedis2.root[self.dataName]['contents']

        if (self.content in list):
            logging.info("deleting {}...".format(self.content[0]))
            list.remove(self.content)
            post = {
                'heads': DataRedis2.root[self.dataName]['heads'],
                'contents': list
            }
            DataRedis2.root[self.dataName] = post
            lock.release()
        else:
            lock.release()
            logging.error(
                f"This Objects {self.dataName} {self.content[0]} has been updated or deleted by others"
            )
            raise (
                f"This Objects {self.dataName} {self.content[0]} has been updated or deleted by others"
            )

    @staticmethod
    def queryAll(dataName, part='contents'):

        return DataRedis2.root[dataName][part]

    @staticmethod
    def initializationRedis(dataName, dataKey, dataArray):

        DataRedis2.root[dataName] = {'heads': dataKey, 'contents': dataArray}
from random import randint
from flask_mqtt import Mqtt
from operator import methodcaller
from threading import Lock
from redisworks import Root
from os import getpid
from datetime import datetime

capitalize = methodcaller('capitalize')

##### Flask APP #####
app = Flask(__name__)
json = FlaskJSON(app)

db = redis.Redis('localhost')
redisDB = Root()

app.config['JSON_ADD_STATUS'] = False
app.config['JSON_JSONP_OPTIONAL'] = True
##################################################

################Init Lock################
lock = Lock()
##########################################

################Init Nodes################
#our Rooms database
USMastersNodeLocation = {
    "Node1": "Desert Suite 1",
    "Node2": "Desert Suite 2",
    "Node3": "Desert Suite 3",
Beispiel #24
0

"""
@version: 0.1
@author: Yang Reid
@license: Apache Licence 
@contact: [email protected]
@site: https://github.com/yangr5/python
@software: PyCharm
@file: app.py
@time: 2019/7/11 10:42
"""
from flask import Flask,render_template,jsonify,json
from redisworks import Root

root = Root(host='10.207.85.40')

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')


@app.route('/ajax/get_dm_array')
def ajax_rigs():
    return jsonify(data=list(root.dm.array))

@app.route('/rigs')
def rigs():
    return render_template('Rig.html')
    def add_properties(self):
        for prop in self.properties:
            if prop:
                pswr_prop = self.punto_switcher(prop)
                if self.is_valid(pswr_prop):
                    self.add_phrase(mut_dict=self.search_words[pswr_prop[0]],
                                    word=prop,
                                    pswr_word=pswr_prop)
                else:
                    self.stop_words.add(prop)


def main(path: str, file_name: str):
    with open(path + file_name, 'r', encoding='utf-8') as csv_file:
        reader = csv.DictReader(csv_file)
        for ix, line in enumerate(reader):
            if not line['gm_name']:
                continue
            db = SuggestDB(**line)
            db()


if __name__ == '__main__':
    root = Root(db=1)
    for file_name in os.listdir(PATH_CSV_FILES):
        main(PATH_CSV_FILES, file_name)
    root.suggest_db = SuggestDB.suggest_db.copy()
    root.search_words_db = SuggestDB.search_words.copy()
    root.stop_words = SuggestDB.stop_words.copy()
Beispiel #26
0
# Copyright (C) 2019 The Raphielscape Company LLC.
# Copyright (C) 2018 - 2019 MrYacha
#
# This file is part of SophieBot.
#
# SophieBot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Licensed under the Raphielscape Public License, Version 1.c (the "License");
# you may not use this file except in compliance with the License.

import redis
from redisworks import Root

from sophie_bot.config import get_str_key, get_int_key

# Init Redis
redis = redis.StrictRedis(
    host=get_str_key("REDIS_HOST"),
    port=get_str_key("REDIS_PORT"),
    db=get_int_key("REDIS_DB_FSM"),
    decode_responses=True
)

rw = Root()
 def __init__(self, host="127.0.0.1", port=6379):
     self.redis = Root(host, port)
     self.wait_connection()
     self.redis.rates = {"ID": {"ID": 1}}
Beispiel #28
0
#!/usr/bin/python
# encoding: utf-8
"""
@author: yangr5 
@contact: [email protected]
@version: 1.0
@license: Apache Licence
@file: redis_test.py
@time: 2019-09-03 15:10

这一行开始写关于本文件的说明与解释
"""
from redisworks import Root
import json

root = Root(host='18.221.249.189')

def main():
    # root.test = [1,2,3]
    with open('data.json') as json_file:
        data = json.load(json_file)
        # print(data)
        root.dm.array = data
    # print(root.test)
    for rig in root.dm.array:
        print(rig)


if __name__ == '__main__':
    main()
Beispiel #29
0
from jinja2config import JinjaConfig
from redisworks import Root
from scrapy import FetchData
import cherrypy
import os


root = Root(host='0.0.0.0', port=6379, db=0)


class Application(object):

    @cherrypy.expose
    def index(self):
        template = JinjaConfig.JINJA_ENVIRONMENT.get_template('index.html')
        return template.render(data=root.data)


cherrypy.config.update({
    'environment': 'production',
    'log.screen': True,
    'server.socket_host': '0.0.0.0',
    'server.socket_port': 8080,
})

conf = {
    '/static': {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': os.path.join(JinjaConfig.BASE_DIR, 'static'),
        'tools.staticdir.content_types': {'html': 'application/octet-stream'}
    }
Beispiel #30
0
 def setUp(self):
     self.root = Root(redis=FakeStrictRedis)
     self.red = FakeStrictRedis()