Best Python code snippet using assertpy_python
models.py
Source:models.py
...21 users = models.ManyToManyField(User, related_name='projects')22 project_type = models.CharField(max_length=30, choices=PROJECT_CHOICES)23 def get_absolute_url(self):24 return reverse('upload', args=[self.id])25 def is_type_of(self, project_type):26 return project_type == self.project_type27 def get_progress(self, user):28 docs = self.get_documents(is_null=True, user=user)29 total = self.documents.count()30 remaining = docs.count()31 return {'total': total, 'remaining': remaining}32 @property33 def image(self):34 if self.is_type_of(self.DOCUMENT_CLASSIFICATION):35 url = staticfiles_storage.url('images/cat-1045782_640.jpg')36 elif self.is_type_of(self.SEQUENCE_LABELING):37 url = staticfiles_storage.url('images/cat-3449999_640.jpg')38 elif self.is_type_of(self.Seq2seq):39 url = staticfiles_storage.url('images/tiger-768574_640.jpg')40 return url41 def get_template_name(self):42 if self.is_type_of(Project.DOCUMENT_CLASSIFICATION):43 template_name = 'annotation/document_classification.html'44 elif self.is_type_of(Project.SEQUENCE_LABELING):45 template_name = 'annotation/sequence_labeling.html'46 elif self.is_type_of(Project.Seq2seq):47 template_name = 'annotation/seq2seq.html'48 else:49 raise ValueError('Template does not exist')50 return template_name51 def get_documents(self, is_null=True, user=None):52 docs = self.documents.all()53 if self.is_type_of(Project.DOCUMENT_CLASSIFICATION):54 if user:55 docs = docs.exclude(doc_annotations__user=user)56 else:57 docs = docs.filter(doc_annotations__isnull=is_null)58 elif self.is_type_of(Project.SEQUENCE_LABELING):59 if user:60 docs = docs.exclude(seq_annotations__user=user)61 else:62 docs = docs.filter(seq_annotations__isnull=is_null)63 elif self.is_type_of(Project.Seq2seq):64 if user:65 docs = docs.exclude(seq2seq_annotations__user=user)66 else:67 docs = docs.filter(seq2seq_annotations__isnull=is_null)68 else:69 raise ValueError('Invalid project_type')70 return docs71 def get_document_serializer(self):72 from .serializers import ClassificationDocumentSerializer73 from .serializers import SequenceDocumentSerializer74 from .serializers import Seq2seqDocumentSerializer75 if self.is_type_of(Project.DOCUMENT_CLASSIFICATION):76 return ClassificationDocumentSerializer77 elif self.is_type_of(Project.SEQUENCE_LABELING):78 return SequenceDocumentSerializer79 elif self.is_type_of(Project.Seq2seq):80 return Seq2seqDocumentSerializer81 else:82 raise ValueError('Invalid project_type')83 def get_annotation_serializer(self):84 from .serializers import DocumentAnnotationSerializer85 from .serializers import SequenceAnnotationSerializer86 from .serializers import Seq2seqAnnotationSerializer87 if self.is_type_of(Project.DOCUMENT_CLASSIFICATION):88 return DocumentAnnotationSerializer89 elif self.is_type_of(Project.SEQUENCE_LABELING):90 return SequenceAnnotationSerializer91 elif self.is_type_of(Project.Seq2seq):92 return Seq2seqAnnotationSerializer93 def get_annotation_class(self):94 if self.is_type_of(Project.DOCUMENT_CLASSIFICATION):95 return DocumentAnnotation96 elif self.is_type_of(Project.SEQUENCE_LABELING):97 return SequenceAnnotation98 elif self.is_type_of(Project.Seq2seq):99 return Seq2seqAnnotation100 def __str__(self):101 return self.name102class Label(models.Model):103 KEY_CHOICES = ((u, c) for u, c in zip(string.ascii_lowercase, string.ascii_lowercase))104 COLOR_CHOICES = ()105 text = models.CharField(max_length=100)106 shortcut = models.CharField(max_length=10, choices=KEY_CHOICES)107 project = models.ForeignKey(Project, related_name='labels', on_delete=models.CASCADE)108 background_color = models.CharField(max_length=7, default='#209cee')109 text_color = models.CharField(max_length=7, default='#ffffff')110 def __str__(self):111 return self.text112 class Meta:113 unique_together = (114 ('project', 'text'),115 ('project', 'shortcut')116 )117class Document(models.Model):118 text = models.TextField()119 project = models.ForeignKey(Project, related_name='documents', on_delete=models.CASCADE)120 def get_annotations(self):121 if self.project.is_type_of(Project.DOCUMENT_CLASSIFICATION):122 return self.doc_annotations.all()123 elif self.project.is_type_of(Project.SEQUENCE_LABELING):124 return self.seq_annotations.all()125 elif self.project.is_type_of(Project.Seq2seq):126 return self.seq2seq_annotations.all()127 def make_dataset(self):128 if self.project.is_type_of(Project.DOCUMENT_CLASSIFICATION):129 return self.make_dataset_for_classification()130 elif self.project.is_type_of(Project.SEQUENCE_LABELING):131 return self.make_dataset_for_sequence_labeling()132 elif self.project.is_type_of(Project.Seq2seq):133 return self.make_dataset_for_seq2seq()134 def make_dataset_for_classification(self):135 annotations = self.get_annotations()136 dataset = [[a.document.id, a.document.text, a.label.text, a.user.username]137 for a in annotations]138 return dataset139 def make_dataset_for_sequence_labeling(self):140 annotations = self.get_annotations()141 dataset = [[self.id, ch, 'O'] for ch in self.text]142 for a in annotations:143 for i in range(a.start_offset, a.end_offset):144 if i == a.start_offset:145 dataset[i][2] = 'B-{}'.format(a.label.text)146 else:...
config.py
Source:config.py
1from dynaconf import Dynaconf, Validator2settings = Dynaconf(3 envvar_prefix="LINKHUB",4 settings_files=["settings.toml", ".secrets.toml"],5 # Validating and setting defaults6 validators=[7 Validator("REQUEST_KEY", is_type_of=str),8 Validator("POLLING_INTERVAL_SECONDS", is_type_of=int, default=5),9 Validator("BOX_ADDRESS", is_type_of=str, default="192.168.1.1"),10 Validator("EXPORTER_PORT", is_type_of=int, default=9877),11 Validator("EXPORTER_ADDRESS", is_type_of=str, default="localhost"),12 ],13)14# `envvar_prefix` = export envvars with `export LINKHUB_FOO=bar`....
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!