timeit

Програмиране с Python

Курс във Факултета по Математика и Информатика към СУ

Решение на Социална мрежа от Яница Велевска

Обратно към всички решения

Към профила на Яница Велевска

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 1 успешни тест(а)
  • 7 неуспешни тест(а)

Код

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import uuid
import datetime


class SocialGraph:
    def check(self, user_uuid):
        bo = False
        for a in self.graph:
            if a[0].uuid == user_uuid:
                b = True
        return bo

    def __init__(self):
        self.graph = []

    def add_user(self, user):
        if self.check(user.uuid):
            raise UserAlreadyExistsError
        self.graph.append([user])

    def get_user(self, user_uuid):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        for a in self.graph:
            if a[0].uuid == user_uuid:
                return a[0]

    def delete_user(self, user_uuid):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        for i in self.graph:
            for b in i:
                if b.uuid == user_uuid:
                    i.remove(b)
        if i[0].uuid == user_uuid:
            self.graph.remove(i)

    def follow(self, follower, followee):
        if not(self.check(follower.uuid)):
            raise UserDoesNotExistError
        if not(self.check(followee.uuid)):
            raise UserDoesNotExistError
        for i in self.graph:
            if i[0].uuid == follower.uuid:
                if not(followee in i):
                    i.append(followee)

    def unfollow(self, follower, followee):
        if not(self.check(follower.uuid)):
            raise UserDoesNotExistError
        if not(self.check(followee.uuid)):
            raise UserDoesNotExistError
        for i in self.graph:
            if i[0].uuid == follower.uuid:
                if followee in i:
                    i.remove(followee)

    def is_following(self, follower, followee):
        if not(self.check(follower.uuid)):
            raise UserDoesNotExistError
        if not(self.check(followee.uuid)):
            raise UserDoesNotExistError
        for i in self.graph:
            if i[0].uuid == follower.uuid:
                return followee in i

    def followers(self, user_uuid):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        foll = {}
        for i in self.graph:
            if user_uuid in i:
                foll.add(i[0].uuid)
        foll.remove(user_uuid)
        return foll

    def following(self, user_uuid):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        foll = {}
        for i in self.graph:
            if user_uuid == i[0].uuid:
                for b in i:
                    foll.add(b.uuid)
        foll.remove(user_uuid)
        return foll

    def friends(self, user_uuid):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        a = self.following(user_uuid)
        b = self.followers(user_uuid)
        for i in a:
            if not(i in b):
                a.remove(i)
        return a

    def nth_layer_followings(self, user_uuid, n):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        if n == 1:
            return self.following(user_uuid)
        foll = {}
        for i in self.following(user_uuid):
            foll.update(nth_layer_following(i, n-1))
        return foll

    def generate_feed(self, user_uuid, offset=0, limit=10):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        feed = []
        for i in self.following(user_uuid):
            feed.extend(i.posts[offset:limit])


class Post:
    def __init__(self, aut, cont):
        self.author = aut
        self.published_at = datatime.datetime.now()
        self.content = cont


class User:
    def __init__(self, name):
        self.name = name
        self.uuid = uuid.uuid4()
        self.posts = []

    def add_post(self, post_content):
        a = Post(self.uuid, post_content)
        self.posts.append(a)
        if len(self.posts) > 50:
            self.posts.pop(0)

    def get_post(self):
        for i in self.posts:
            yield i


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass

Лог от изпълнението

FEEEEEE.
======================================================================
ERROR: test_distnaces (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AttributeError: 'UUID' object has no attribute 'uuid'

======================================================================
ERROR: test_feed (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AttributeError: 'UUID' object has no attribute 'uuid'

======================================================================
ERROR: test_following (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AttributeError: 'UUID' object has no attribute 'uuid'

======================================================================
ERROR: test_friends (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AttributeError: 'UUID' object has no attribute 'uuid'

======================================================================
ERROR: test_layer_followings (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AttributeError: 'UUID' object has no attribute 'uuid'

======================================================================
ERROR: test_add_post (test.TestUser)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
NameError: name 'datatime' is not defined

======================================================================
FAIL: test_add_get_and_delete_user (test.TestSocialGraph)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: UserAlreadyExistsError not raised

----------------------------------------------------------------------
Ran 8 tests in 0.060s

FAILED (failures=1, errors=6)

История (1 версия и 0 коментара)

Яница обнови решението на 18.04.2016 13:39 (преди над 1 година)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import uuid
import datetime


class SocialGraph:
    def check(self, user_uuid):
        bo = False
        for a in self.graph:
            if a[0].uuid == user_uuid:
                b = True
        return bo

    def __init__(self):
        self.graph = []

    def add_user(self, user):
        if self.check(user.uuid):
            raise UserAlreadyExistsError
        self.graph.append([user])

    def get_user(self, user_uuid):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        for a in self.graph:
            if a[0].uuid == user_uuid:
                return a[0]

    def delete_user(self, user_uuid):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        for i in self.graph:
            for b in i:
                if b.uuid == user_uuid:
                    i.remove(b)
        if i[0].uuid == user_uuid:
            self.graph.remove(i)

    def follow(self, follower, followee):
        if not(self.check(follower.uuid)):
            raise UserDoesNotExistError
        if not(self.check(followee.uuid)):
            raise UserDoesNotExistError
        for i in self.graph:
            if i[0].uuid == follower.uuid:
                if not(followee in i):
                    i.append(followee)

    def unfollow(self, follower, followee):
        if not(self.check(follower.uuid)):
            raise UserDoesNotExistError
        if not(self.check(followee.uuid)):
            raise UserDoesNotExistError
        for i in self.graph:
            if i[0].uuid == follower.uuid:
                if followee in i:
                    i.remove(followee)

    def is_following(self, follower, followee):
        if not(self.check(follower.uuid)):
            raise UserDoesNotExistError
        if not(self.check(followee.uuid)):
            raise UserDoesNotExistError
        for i in self.graph:
            if i[0].uuid == follower.uuid:
                return followee in i

    def followers(self, user_uuid):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        foll = {}
        for i in self.graph:
            if user_uuid in i:
                foll.add(i[0].uuid)
        foll.remove(user_uuid)
        return foll

    def following(self, user_uuid):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        foll = {}
        for i in self.graph:
            if user_uuid == i[0].uuid:
                for b in i:
                    foll.add(b.uuid)
        foll.remove(user_uuid)
        return foll

    def friends(self, user_uuid):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        a = self.following(user_uuid)
        b = self.followers(user_uuid)
        for i in a:
            if not(i in b):
                a.remove(i)
        return a

    def nth_layer_followings(self, user_uuid, n):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        if n == 1:
            return self.following(user_uuid)
        foll = {}
        for i in self.following(user_uuid):
            foll.update(nth_layer_following(i, n-1))
        return foll

    def generate_feed(self, user_uuid, offset=0, limit=10):
        if not(self.check(user_uuid)):
            raise UserDoesNotExistError
        feed = []
        for i in self.following(user_uuid):
            feed.extend(i.posts[offset:limit])


class Post:
    def __init__(self, aut, cont):
        self.author = aut
        self.published_at = datatime.datetime.now()
        self.content = cont


class User:
    def __init__(self, name):
        self.name = name
        self.uuid = uuid.uuid4()
        self.posts = []

    def add_post(self, post_content):
        a = Post(self.uuid, post_content)
        self.posts.append(a)
        if len(self.posts) > 50:
            self.posts.pop(0)

    def get_post(self):
        for i in self.posts:
            yield i


class UserDoesNotExistError(Exception):
    pass


class UserAlreadyExistsError(Exception):
    pass