class Film(MovieModel): __primarylabel__ = "Movie" __primarykey__ = "title" awesome = Label() musical = Label() science_fiction = Label(name="SciFi") title = Property() tag_line = Property(key="tagline", default="Bit boring") year_of_release = Property(key="released") actors = RelatedFrom("Person", "ACTED_IN")
class Thing(GraphObject): __primarykey__ = "name" p = Label() q = Label() x = Related("Thing", "X") y = Related("Thing", "Y") x_out = RelatedTo("Thing", "X") y_out = RelatedTo("Thing", "Y") x_in = RelatedFrom("Thing", "X") y_in = RelatedFrom("Thing", "Y")
class Politician(GraphObject): name = Property() male = Label() female = Label() belongs = RelatedTo(PoliticalParty) mayor = RelatedTo(Institution) def __init__(self, name, gender, political_party_name, region_name, institution_name, charge, salary): self.name = name if (gender == 'female'): self.female = True else: self.male = True if (political_party_name): political_party = PoliticalParty(political_party_name) self.belongs.add(political_party) if (institution_name and charge): institution = Institution(institution_name, region_name) if (charge == 'Alcalde'): self.mayor.add(institution, salary=salary) def getPoliticalParty(self): politicalPartyRelationship = self.belongs._related_objects[0] return politicalPartyRelationship[0] def getInstitution(self): institution = self.mayor._related_objects[0] return institution[0] def getSalary(self): institution = self.mayor._related_objects[0] return institution[1]['salary'] def serialize(self): institution = self.getInstitution() return { 'id': self.__node__.identity, 'name': self.name, 'gender': 'male' if self.male else 'female', 'politicalParty': self.getPoliticalParty().serialize(), 'mayor': { 'institution': institution.serialize(), 'salary': self.getSalary() } }
class Film(MovieGraphObject): __primarylabel__ = "Movie" __primarykey__ = "title" awesome = Label() musical = Label() science_fiction = Label(name="SciFi") title = Property() tag_line = Property(key="tagline") year_of_release = Property(key="released") actors = RelatedFrom("Person", "ACTED_IN") def __init__(self, title): self.title = title
class Asset(GraphObject): asset = Label("Asset") __primarykey__ = "serial_number" name = Property() serial_number = Property() tag = Property() date_captured = Property()
class Room(GraphObject): __primarykey__ = "name" room = Label("Room") name = Property() detail = RelatedTo("RoomDetail") date_updated = Property('updatedOn') category = RelatedTo("Category", "HAS_CATEGORY")
class Tech(GraphObject): ___primarykey__ = 'name' Assoc_ID = Property() Skill_ID = Property() Skill_Name = Property() Exp_Level = Property() tech = Label() person_knows = RelatedFrom("Person", "KNOWS")
class RoomDetail(GraphObject): __primarykey__ = "room" room_detail = Label("RoomDetail") seat_count = Property() white_board = Property() projector = Property() date_updated = Property('updatedOn') room = RelatedTo("Room", "HAS_DETAIL")
class Person(GraphObject): person = Label("Person") __primarykey__ = "serial_number" title = Property() firstname = Property() lastname = Property() mobile_number = Property('mobileNumber') email_address = Property('emailAddress') date_updated = Property('updatedOn')
class StreamerNode(UserGraphObj): __primarylabel__ = 'Streamer' # PROPERTIES profile_img_url = Property() # LABELS # A Label defined on a GraphObject provides an accessor to a label of the underlying central node. # It is exposed as a boolean value, the setting of which allows the label to be toggled on or off. streamer = Label('Streamer') is_followed_by = RelatedFrom('User', 'IS_FOLLOWED_BY') is_following = RelatedTo('Streamer', 'IS_FOLLOWING')
class Category(GraphObject): __primarykey__ = "name" category = Label("Category") name = Property() description = Property() room = RelatedTo("Room", "HAS_CATEGORY") def __str__(self): return f'{self.name}'
class Diagnosis(GraphObject): __primarylabel__ = "Diagnosis" evaluation = Label(name="EVALUATION") name = Property() diag_disease = RelatedTo("Disease") def to_dict(self): return { 'name': self.name, }
class Event(GraphObject): appointment = Label("Appointment") name = Property() subject = Property() agender = Property() start_date = Property('startsOn') end_date = Property('endsOn') organizer = Related(Person) attendee = RelatedTo(Person) room = Related(Room) date_updated = Property('updatedOn')
class Person(GraphObject): ___primarykey__ = 'name' Assoc_ID = Property() Name = Property() Desig = Property() Dob = Property() Doj = Property() Loc_City = Property() Loc_Cntry = Property() person = Label() knows = RelatedTo("Tech")
#coding: utf-8 from py2neo import Graph, Node, Relationship from py2neo.matching import NodeMatcher, RelationshipMatcher from py2neo.ogm import Label import re import xlrd # 连接neo4j数据库 graph = Graph("http://127.0.0.1:7474",username="******",password="******") industry_category_layer4_label = Label() concept_block_label = Label() province_label = Label() # name属性是label的唯一标识 industry_category_layer4_label.name = "行业四级分类" concept_block_label.name = "概念板块" province_label.name = "地域板块" file_name = 'A股板块分类-V1.xlsx' workbook = xlrd.open_workbook(file_name) sheet_names= workbook.sheet_names() industry_category_relation = "公司属于行业" concept_block_relation = "公司属于概念板块" province_block_relation = "公司属于地域板块" product_lable = "产品类型" common_category = [] def load_industry_category_layer4(sheet): row_count = 0 new_industry_category_node_count = 0
class User(BaseModel): """ Constructs an abstract representation (GraphObject) of a record in database, a User Node. Can be used with `Graph` to match, update, or create a record in the neo4j DB. The streamer `Label` is a boolean value that allows the "Streamer" label to be toggled on/off for a Node. By default, GraphObjects represent User nodes. A `Relationship` describes how User and Streamer nodes are associated. """ # The primary property key used for Cypher MATCH and MERGE operations __primarykey__ = 'twitch_uid' # PROPERTIES twitch_uid = Property() name = Property() display_name = Property() profile_img_url = Property() broadcaster_type = Property() total_followers = Property() total_followings = Property() # LABELS # A Label defined on a GraphObject provides an accessor to a label of the underlying central node. # It is exposed as a boolean value, the setting of which allows the label to be toggled on or off. streamer = Label('Streamer') # RELATIONSHIPS is_followed_by = RelatedFrom('User', 'FOLLOWS') @staticmethod def create_or_update_from_twitch_client(some_streamer: TwitchStreamer): db_streamer = User() try: for key, val in some_streamer.as_dict().items(): db_streamer.__setattr__(key, val) # TODO: Note that this updates an existing node; this may be undesirable if we want to compare # total followers reported by Twitch to record in DB and perform an action # Add or Update in DB db_streamer.save() module_logger.info("Added {} to DB".format(some_streamer.display_name)) except Exception as exc: module_logger.error("Failed to add streamer to db: {}".format(some_streamer.display_name)) module_logger.error(exc) return db_streamer @staticmethod def add_followers_to_streamer(some_streamer: TwitchStreamer, db_streamer: GraphObject) -> dict: """ Adds all followers to db for a given TwitchStreamer object and py2neo ogm object :param some_streamer: TwitchStreamer object and related methods :param db_streamer: An GraphObject representation of a Streamer node in the DB :return: List of follower id's obtained from twitch """ foll_list = some_streamer.get_all_follows() foll_nodes = [] for each_fol in foll_list: db_follower = User() try: db_follower.twitch_uid = each_fol['from_id'] db_follower.display_name = each_fol['from_name'] # Commit to new follower to DB db_follower.save() foll_nodes.append(db_follower) # Add Followed-by relationship to Streamer Node db_streamer.is_followed_by.update( db_follower, properties={'followed at': each_fol['followed_at']} ) except Exception as exc: module_logger.error("Failed to add follower to db: {}".format(each_fol['from_name'])) module_logger.error(exc) pass # Save all updated follower references to DB db_streamer.save() # Return a list of follower id's from twitch return {'foll_id_list': [each_fol['from_id'] for each_fol in foll_list], 'foll_nodes': foll_nodes} @staticmethod def add_all_followers_followings(foll_dict: dict): for follower_id, foll_node in zip(foll_dict['foll_id_list'], foll_dict['foll_nodes']): # Fetch a followings list from twitch all_followings = twitch_client.get_all_follows(follower_id, 'from_id', skip_validation=True) for some_followed_stream in all_followings: # Attempt to find Streamer node in DB matched_streamer = User.match(graph, some_followed_stream['to_id']).first() if matched_streamer is None: new_streamer = TwitchStreamer(some_followed_stream['to_name']) matched_streamer = User.create_or_update_from_twitch_client(new_streamer) matched_streamer.is_followed_by.update( foll_node, properties={'followed at': some_followed_stream['followed_at']} ) matched_streamer.save()