AWS |
AWS Partner Network (APN) Blog |
Accelerate SaaS Delivery onto DoD Networks with Game Warden from Second Front Systems |
https://aws.amazon.com/blogs/apn/accelerate-saas-delivery-onto-dod-networks-with-game-warden-from-second-front-systems/
|
Accelerate SaaS Delivery onto DoD Networks with Game Warden from Second Front SystemsSoftware as a service products have revolutionized private sector business operations in recent years but despite the commercial sector s demonstrated success with SaaS the U S Department of Defense DoD has been slow to adopt this new delivery model Learn how the Game Warden platform built by Second Front Systems a DoD compliant DevSecOps platform as a service that accelerates software delivery onto DoD networks while adhering to stringent cybersecurity controls |
2023-08-14 18:32:20 |
AWS |
AWS Security Blog |
Cost considerations and common options for AWS Network Firewall log management |
https://aws.amazon.com/blogs/security/cost-considerations-and-common-options-for-aws-network-firewall-log-management/
|
Cost considerations and common options for AWS Network Firewall log managementWhen you re designing a security strategy for your organization firewalls provide the first line of defense against threats Amazon Web Services AWS offers AWS Network Firewall a stateful managed network firewall that includes intrusion detection and prevention IDP for your Amazon Virtual Private Cloud VPC Logging plays a vital role in any firewall policy as … |
2023-08-14 18:47:47 |
AWS |
AWS Security Blog |
Cost considerations and common options for AWS Network Firewall log management |
https://aws.amazon.com/blogs/security/cost-considerations-and-common-options-for-aws-network-firewall-log-management/
|
Cost considerations and common options for AWS Network Firewall log managementWhen you re designing a security strategy for your organization firewalls provide the first line of defense against threats Amazon Web Services AWS offers AWS Network Firewall a stateful managed network firewall that includes intrusion detection and prevention IDP for your Amazon Virtual Private Cloud VPC Logging plays a vital role in any firewall policy as … |
2023-08-14 18:47:47 |
python |
Pythonタグが付けられた新着投稿 - Qiita |
【Twitter改めX】詐欺アカウント自動ブロックツール作成③ |
https://qiita.com/nobukz/items/953a18b7b76a2557d5db
|
twitter |
2023-08-15 03:12:11 |
海外TECH |
Ars Technica |
Dealmaster: Early Labor Day savings on Apple, Lenovo, Kindle, and Google tech |
https://arstechnica.com/?p=1960645
|
pixel |
2023-08-14 18:31:48 |
海外TECH |
MakeUseOf |
How to Make a Custom Watch Face From a Photo on Your Apple Watch |
https://www.makeuseof.com/how-to-make-custom-apple-watch-face-from-photo/
|
apple |
2023-08-14 18:46:20 |
海外TECH |
MakeUseOf |
4 Reasons Why the Xbox Series S Won't Get a Disc Drive (Despite a Patent Being Claimed for It) |
https://www.makeuseof.com/xbox-series-s-wont-get-a-disc-drive-reasons-why/
|
Reasons Why the Xbox Series S Won x t Get a Disc Drive Despite a Patent Being Claimed for It The Xbox Series S is a great digital only console but could we see a disc drive coming to it Despite a patent implying as such we think not |
2023-08-14 18:31:20 |
海外TECH |
MakeUseOf |
How to Calculate a Folder's Size Using PowerShell on Windows |
https://www.makeuseof.com/calculate-folder-size-powershell/
|
windows |
2023-08-14 18:15:21 |
海外TECH |
DEV Community |
How to add user auth with Clerk to Nextjs (App Directory) and store it in Embedded Sanity CMS |
https://dev.to/julimancan/how-to-add-user-auth-with-clerk-to-nextjs-app-directory-and-store-it-in-embedded-sanity-cms-f0
|
How to add user auth with Clerk to Nextjs App Directory and store it in Embedded Sanity CMS How to add user auth with Clerk to Nextjs App Directory and store it in Sanity CMSFirst we are going to install a new Nextjs project but this should work with any Nextjs App Directory project then we will install Clerk and get it working once it s working we will install and embed a Sanity studio and get it ready to receive the information provided by Clerk We will finish by creating an API route where we will send the user data to Sanity and verify that the user was created Install Nextjs in an empty folder npx create next app latest Proceed with you favourite settings I will choose Tailwind Typescript and the App Directory For the purpose of this tutorial I want to start with an empty home page in app page tsx app page tsxconst Home gt return lt main className grid place content center min h screen gt Hello World lt main gt export default Home Run the project and verify everything is workingnpm run devInstall Clerk and dependencies npm install clerk nextjsGo to www clerk com and sign up for a free account Once inside click on Add Application and give it a nameSelect your favorite sign in providers I will choose email and Google Copy the API Keys and paste them into your env local file Mount ClerkProviderUpdate your root layout to include the wrapper The component wraps your Next js application to provide active session and user context to Clerk s hooks and other components It is recommended that the wraps the to enable the context to be accessible anywhere within the app Your app layout tsx should look something like this app layout tsximport globals css import Inter from next font google import ClerkProvider from clerk nextjs const inter Inter subsets latin export const metadata title Create Next App description Generated by create next app export default function RootLayout children children React ReactNode return lt ClerkProvider gt lt html lang en gt lt body className inter className gt children lt body gt lt html gt lt ClerkProvider gt Protect your ApplicationNow that Clerk is installed and mounted in your application it s time to decide which pages are public and which need to hide behind authentication We do this by creating a middleware ts file at the root folder or inside src if that is how you set up your app In my case I will create the middleware ts file inside the root folder of my project I will also add a Public Routes array so any route you would like to be public you will need to add to this array We will add the studio route and let sanity handle the auth for the CMS studio middlewate tsimport authMiddleware from clerk nextjs This example protects all routes including api trpc routes Please edit this to allow other routes to be public as needed See for more information about configuring your middlewareconst publicRoutes studio export default authMiddleware publicRoutes export const config matcher next api trpc Create the sign up page In the app directory create a sign up folder and then another catch all …sign up folder with a page inside of it Your route should look like this app sign up …sign up page tsxInside the page tsx file we will add the provided SignUp component from Clerk app sign up …sign up page tsximport SignUp from clerk nextjs export default function Page return lt main className grid place content center min h screen gt lt SignUp gt lt main gt Do the same for the Sign in page But we will also add the redirectUrl parameter provided by nextjs so that the user can be redirected back to the page they were visiting before or home if there isn t any app sign up …sign in page tsximport SignIn from clerk nextjs export default function Page searchParams searchParams redirectUrl string undefined const redirectUrl searchParams return lt main className grid place content center min h screen gt lt SignIn redirectUrl redirectUrl gt lt main gt Next we will add some more environment variables to control the behavior of clerk and also to tell it what route the sign up and sign in pages are in we do this in the env local file in the root right after our API keys env localNEXT PUBLIC CLERK SIGN IN URL sign inNEXT PUBLIC CLERK SIGN UP URL sign upNEXT PUBLIC CLERK AFTER SIGN IN URL NEXT PUBLIC CLERK AFTER SIGN UP URL Create a link to the sign up page inside the homepage In app page tsx lets add the linkRun the app navigate to http localhost using the browser and click on sign up sign up for your app app page tsximport Link from next link const Home gt return lt main className grid place content center min h screen gt lt Link href sign up gt Sign Up lt Link gt lt main gt export default Home Go to dashboard clerk com select your project and verify your new user In the homepage let s add some user data to the home page and some conditional rendering to see the data if the user is logged in app page tsximport UserButton currentUser from clerk nextjs import Link from next link export default async function Home const user await currentUser if user return lt main className grid place content center gap min h screen gt lt div className flex items center gap gt lt UserButton afterSignOutUrl gt lt h gt Welcome back user firstName lt h gt lt div gt lt main gt return lt main className grid place content center min h screen gt lt Link href sign up gt Sign Up lt Link gt lt main gt Let s visit our homepage If we click on the user image we have access to some options for now I will try to sign out to see the other view You should see your sign up button now We can also add a signout button provided by clerk to make it easier to sign out and clearer that its a sign up sign in page as well app page tsximport currentUser UserButton SignOutButton from clerk nextjs import Link from next link export default async function Home const user await currentUser if user return lt main className grid place content center gap min h screen gt lt div className flex items center gap gt lt UserButton afterSignOutUrl gt lt h gt Welcome back user firstName lt h gt lt SignOutButton gt lt div gt lt main gt return lt main className grid place content center min h screen gt lt Link href sign up gt Sign Up Sign In lt Link gt lt main gt Now that we have sign up and sign out flows setup let s create and embed a new sanity CMS in our Nextjs project this process will still work even if the CMS isn t embedded however I will embed a new project for the purpose of this tutorial Install the necessary packages for Sanity to work as well as the client packages to make the requests to the CMS npm install sanity next sanity next sanity image portabletext react sanity client sanity image urlGo to sanity io sign up for an account Sanity gives instructions on how to create the studio however since we are embedding the studio on a Nextjs project we can just ignore them and navigate to https www sanity io manage If Sanity created a project for you click on it and copy the project ID if they didn t you can click on Create a new project on the top and then copy the project ID provided Go to your env local and add the project id under the Clerk variables like this env localSANITY PROJECT ID your sanity project id Create a sanity folder on the root of your project and a config ts file with the following content sanity config tsexport const sanityConfig projectId process env SANITY PROJECT ID dataset production useCdn process env NODE ENV production apiVersion Create your studioConfig ts file inside the sanity folder in it for now we will define the config and the basePath for the sanity studio sanity studioConfig tsimport sanityConfig from config import defineConfig from sanity export const sanityAdminConfig sanityConfig title Clerk Users and Sanity Test basePath admin export const studioConfig defineConfig sanityAdminConfig Inside App directory create a page studio route and an index catch all filder where we will mount the studio app studio …index page tsx We will import the studio config we just created and give it as a prop to the NextStudio component provided by Sanity this is Client component so we will have to add the use client directive at the top of the file app studio …index page tsx use client import studioConfig from sanity studioConfig import NextStudio from next sanity studio export default function StudioPage Supports the same props as import Studio from sanity config is required return lt NextStudio config studioConfig gt Lets restart our project by running npm run dev and navigate to http localhost studioWe should get this error This is because we need to allow the route in the Sanity studio for it to be able to read and write to sanity s database We can add CORS origins in the Sanity Management Dashboard for the project Go to open the project we created previously and navigate to the API tab and look for the Add CORS origin buttonAdd http localhost and click Allow Credentials since we are embedding the studio in our NextJS project we need to allow it to have credentials When you deploy your site you will need to add the root address of your site ie to the list of allowed sites Click Save We can also delete localhost since this is the location of the default sanity studio which we are not using Go back to http localhost studio and sign in with your user Now we have an empty studio embedded in the Nextjs site Let s add a users document list to our sanity Create the schema for the users for our tutorial we will just need first name last name and email Create a schemas folder inside the sanity folder and create user ts sanity schemas user ts add the document for users we will use an icon from sanity so that the users list gets a nice icon sanity schemas user tsimport defineType defineField from sanity import UserIcon from sanity icons export const userSchema defineType name user title Users type document icon UserIcon fields defineField name firstName title First Name type string defineField name lastName title Last Name type string defineField name email title Email type string Now lets add the schemas to our studioConfig I like to keep my schemas organized in a separate file so that it s easy to add or remove schemas from my project Create in the sanity folder a schemas ts file sanity schema ts and add the following sanity schema tsimport userSchema from schemas user export const schemaTypes userSchema Import the schemas into the sanity studioConfig ts file and add it to the config sanity studioConfig tsimport sanityConfig from config import defineConfig from sanity import schemaTypes from schemas export const sanityAdminConfig sanityConfig title Clerk Users and Sanity Test basePath studio schema types schemaTypes export const studioConfig defineConfig sanityAdminConfig We are still getting the same screen that s because I forgot to add the desktool to the config let s do that sanity studioConfig ts import sanityConfig from config import defineConfig from sanity import schemaTypes from schemas import deskTool from sanity desk export const sanityAdminConfig sanityConfig title Clerk Users and Sanity Test basePath studio plugins deskTool schema types schemaTypes export const studioConfig defineConfig sanityAdminConfig Refresh and we should be able to see the users list and we can add users to our list However we want them to be added when the user signs in with our site To do that we will use Clerks environment variables to redirect the user after sign up to an API route where we can get the user s information add save it in our sanity and redirect the user again either to where they where coming from or to the homepage Let s start by creating the API route first Create a folder create sanity user you can name this something relevant to your app inside the app folder and create a file route tsx This is a special Nextjs file that let s us create api end points app create sanity user route ts in it we will use clerk s currentUser server function to access the user information We also want to prevent anyone from accessing the route so if the user doesn t exist then we can redirect them to the sign in page however since we are using the middleware ts file we won t need to add that to our api route Let s try to return the user s data first app create sanity user route tsimport currentUser from clerk nextjs import NextResponse from next server export const GET async gt const user await currentUser return NextResponse json user Navigate to http localhost create sanity user we should now see the data that clerk gives you access too we can save any of it in our sanity Navigate to http localhost http localhost and log out and navigate back to http localhost create sanity user and we should now get redirected to the sign in page Great Let s log back in and we should get redirected back to create sanity user end point Let s now add the user data to sanity we need to create a sanityClient file which will allow us to have access to sanity s functionality and be able to read and write data from our nextjs app We will also need an API token from sanity to be able to write data onto it Go back to https www sanity io manage and in the API tab click on Add API tokenGive it a name and select the Editor option to have access to read and write token options Copy your token and add it to your env local file jsxSANITY API TOKEN your api token Create in the sanity folder a sanityClient ts file and import the createClient function from next sanity and your own config and just add the token from the environment variables or empty if it doesn t exist jsx sanity sanityClient tsimport createClient from next sanity import sanityConfig from config export default createClient sanityConfig token process env SANITY API TOKEN Go to the app create sanity user route ts file and import the sanityClient we just created we will use the createIfNotExists which prevents errors when a user already exists function that the next sanity package provides to create a document in our sanity We are also going to get the base path URL to redirect to from the request this will work for both development and deployed since the NextResponse redirect function expects a full url And redirect the user to this base URL Even though the middleware is taking care of authentication we still want to prevent typescript errors in case the user doesn t exist jsx app create sanity user route tsimport NextApiRequest from next import sanityClient from sanity sanityClient import currentUser from clerk nextjs import NextResponse from next server export const GET async req NextApiRequest gt const user await currentUser if user return NextResponse redirect sign in const id firstName lastName emailAddresses user await sanityClient createIfNotExists type user id id firstName lastName email emailAddresses emailAddress const url req url split create sanity user return NextResponse redirect url Restart your server and navigate to http localhost create sanity user then navigate to your studio at http localhost studio and see the new user has been added to your studioThe last step will be to update the NEXT PUBLIC CLERK AFTER SIGN UP URL environment variable and point it to create sanity user route to our environment variables so Clerk can have the right behavior after signing up jsxNEXT PUBLIC CLERK AFTER SIGN UP URL create sanity userRestart your server since we updated the environment variables and try to signup with a different user then check your studio and verify the new user was added That s it now you can add your own specific details to your users or relate it to other documents specific for your use case |
2023-08-14 18:32:34 |
海外TECH |
DEV Community |
Create Custom Block in Quill like Video, Link, Banner. |
https://dev.to/deepakjaiswal/create-custom-block-in-quill-like-video-link-banner-1k13
|
Create Custom Block in Quill like Video Link Banner i am creating this post as my experience sometime we need to create custom block in quill to create block for our need i create some block index html lt div class id editor wrapper keyup keyup event click keyup event gt lt div gt lt div gt lt button click download gt Download lt button gt lt div gt lt div gt lt button click insert gt Insert Hr lt button gt lt div gt lt div id customDropdown style display none gt lt Add your dropdown options here gt lt button gt Option lt button gt lt button gt Option lt button gt lt button gt Option lt button gt lt div gt editor tsdeclare const Quill any import DomSanitizer SafeHtml from angular platform browser declare const quillBetterTable any declare const htmlToPdfmake any declare const pdfMake any import jsPDF from jspdf Quill register modules better table quillBetterTable true const Link Quill import formats link const BlockEmbed Quill import blots block embed class VideoBlot extends BlockEmbed static create obj any console log obj let node super create obj url obj url obj let iframe document createElement iframe let con document createElement span con setAttribute style display flex con setAttribute class resize container con innerHTML lt button data size class re gt x lt button gt lt button data size class re gt x lt button gt lt button data size class re gt x lt button gt lt button data size class re gt x lt button gt con addEventListener click e any gt node setAttribute data width embed responsive e target dataset size console log first e target dataset size node setAttribute data width obj size obj size embed responsive Set styles for wrapper node setAttribute class embed responsive embed responsive by node appendChild con Set styles for iframe iframe setAttribute frameborder iframe setAttribute allowfullscreen true iframe setAttribute src obj url obj url obj Append iframe as child to wrapper node appendChild iframe return node static value domNode any const url domNode getElementsByTagName iframe getAttribute src const size domNode getAttribute data width return url size format name any value any Override the format method to handle your custom attribute if name custom attribute const previousValue this domNode getAttribute data custom attribute if value this domNode setAttribute data custom attribute value else this domNode removeAttribute data custom attribute Use the previousValue as needed else super format name value console log Previous value name value VideoBlot blotName video VideoBlot tagName div Quill register VideoBlot true var CustomLink Quill import formats link CustomLink sanitize function url any return url Customize the URL sanitization if needed class CustomLinkFormat extends CustomLink static create value any const node super create value url console log value node setAttribute data custom attribute value customAttribute Set the custom attribute return node static formats node any const format super formats node console log node format customAttribute node getAttribute data custom attribute Get the custom attribute return format Quill register CustomLinkFormat true var Inline Quill import blots inline Define the custom format classclass CustomFormat extends Inline static create v any const node super create const d document createElement sup d classList add custom format d appendChild node return d Assign a CSS class name to the custom formatCustomFormat blotName highlight CustomFormat tagName span Register the custom format with QuillQuill register CustomFormat Extend ListContainer module const Block Quill import blots block class CustomListContainer extends Block static create value any const node super create value node classList add custom list container return node CustomListContainer tagName div CustomListContainer allowedChildren Block CustomListContainer CustomListContainer scope Block scope CustomListContainer defaultChild block Override the default ListItem module to use the custom list container const ListItem Quill import formats list class CustomListItem extends ListItem format name any value any if name list amp amp value const isOrdered value ordered const CustomContainer any isOrdered OL UL const container any this parent if container instanceof CustomContainer const newContainer this scroll create CustomContainer container replaceWith newContainer newContainer appendChild this domNode super format name value import Component OnInit from angular core Component selector app rich editor templateUrl rich editor component html styleUrls rich editor component css export class RichEditorComponent implements OnInit data any lt p gt lt br gt lt p gt quill any constructor public sanitizer DomSanitizer ngOnInit void setTimeout gt this initEditor document querySelector re addEventListener click gt console log sadasa initEditor this quill new Quill editor wrapper theme snow modules toolbar container video image link align customLink script sub script super list ordered list bullet handlers customLink v any gt console log v this quill format link url customAttribute custom value table false disable table module better table operationMenu items unmergeCells text Another unmerge cells name keyboard bindings quillBetterTable keyboardBindings var customDropdown any document getElementById customDropdown var dropdownOpen false this quill on text change delta any oldDelta any source any gt console log delta oldDelta source var range this quill getSelection if range amp amp range length var cursorPosition range index var lineText this quill getText cursorPosition Define the trigger text or condition to open the dropdown var triggerText Example Open the dropdown when the user types dropdown if lineText endsWith triggerText if dropdownOpen Get the bounds of the current cursor position var bounds this quill getBounds range index Get the offset position of the Quill editor var editorBounds any document getElementById editor wrapper getBoundingClientRect var editorOffsetTop editorBounds top window pageYOffset var editorOffsetLeft editorBounds left window pageXOffset Position the dropdown below the cursor considering the editor offset customDropdown style left bounds left editorOffsetLeft px customDropdown style top bounds top editorOffsetTop bounds height px customDropdown style display block dropdownOpen true else if dropdownOpen Close the dropdown customDropdown style display none dropdownOpen false document addEventListener click function event Close the dropdown if a click event occurs outside the dropdown if customDropdown contains event target customDropdown style display none dropdownOpen false click x any console log x download let x this quill root innerHTML const htmlString lt ol gt lt li class child gt Item lt li gt lt li gt Item lt li gt lt ol gt lt ol gt lt li class chi gt Item lt li gt lt li gt Item lt li gt lt ol gt Replace lt ol gt tags with lt ul gt tags for child elements with the class child x x replaceAll lt ol b gt gt lt li s data list bullet gt lt li gt lt ol gt gi lt ul gt lt ul gt var html htmlToPdfmake x console log html var docDefinition content html styles var pdfDocGenerator pdfMake createPdf docDefinition pdfDocGenerator download var doc any new jsPDF doc html this quill root innerHTML callback function docs any docs save quill content pdf insert const range this quill getSelection const is this quill getFormat range index range length console log is if is highlight this quill format highlight false else this quill format highlight true keyup event any console log event style css dropdown position relative display inline block dropdown toggle padding px px background color fff border px solid ccc border radius px cursor pointer dropdown menu position absolute top left display none min width px padding px background color fff box shadow px px rgba z index dropdown menu show display block dropdown item display block padding px px color text decoration none transition background color s dropdown item hover background color fff |
2023-08-14 18:09:01 |
海外TECH |
DEV Community |
Quick Guide On How To Create a Generative AI Mobile App with Flutter & OpenAI GPT-4 |
https://dev.to/yatendra2001/quick-guide-on-how-to-create-a-generative-ai-mobile-app-with-flutter-openai-gpt-4-6j5
|
Quick Guide On How To Create a Generative AI Mobile App with Flutter amp OpenAI GPT If you ve been itching to create a mobile app that uses the power of Generative AI you re in the right place Let s dive into how you can build one using Flutter and OpenAI GPT And don t worry I ll keep it simple Ready Let s get started Setting Up Your Developer Playground ️First things first let s get your developer environment all set up Flutter SDK If you haven t already grab the latest Flutter SDK It s the backbone of what we re doing IDE Magic Choose your favorite IDE and sprinkle in some Flutter plugins Personally I m a big fan of VSCode But hey you do you Testing Testing Always test your app Use both an emulator and a real device It s like trying on shoes you gotta make sure they fit everywhere Crafting That Sleek Client UI Now let s talk looks Starting Point Kick things off with Flutter s basic app template Here s a nifty command to get you going flutter create org com example t skeleton exampleInspiration Time Not sure about the design No worries Check out dribble or behance They re like the Pinterest of UI design Then bring your vision to life with Flutter s widgets User Experience Remember your app is for users Design a clean UI where they can easily input their queries and marvel at the AI generated content Making Friends with OpenAI GPT This is where the magic happens Getting Started First sign up with OpenAI and secure your API key It s like your golden ticket API Calls Use Flutter s http package to chat with OpenAI The Art of Prompting Think of prompts as the questions you ask the AI The better the question the better the answer So be specific Instead of just saying Translate go with Translate the following English text to French Test and refine your prompts in the OpenAI playground Need some tips Here s a handy GPT best practices guide Customizing Your AI Want your AI to be a specialist Fine tuning is the answer Gather data related to your app like chat logs or specific content Then use OpenAI s platform to fine tune your model Once done integrate it into your app Here s a GPT Fine Tuning guide to help you out Making It Shine on Android amp iOS Your app needs to look and work great whether it s on an Android or an iPhone Design Nuances Each platform has its quirks Get familiar with them Native Functionalities Use Flutter s platform channels to tap into native features Consistency is Key Ensure your app feels the same across both platforms Launching Your Masterpiece You ve built it now let s get it out there Building for Production Use Flutter s tools to get your app ready for the big leagues Store Time Launch your app on the Google Play Store amp Apple App Store Let the world see your creation Feedback Loop Always keep an ear out for user feedback It s how you make your app even better Conclusion Whew That was a ride wasn t it Sure there s always more to add and learn but this guide should set you on the right path Thanks for sticking with me till the end If you found this helpful give me a follow for more such guides And if you re scratching your head over something drop a comment Let s chat Before we go I m building an online school for budding developers like you Every week there s a new podcast tutorial or coding tip waiting for you Let s make you a x developer Videos will start dropping soon About me I am a coder with a keen interest in fixing real world problems through shipping tech products I love to read books I have read multiple books on start ups and productivity Some of my favourite reads are Zero to One Steve Jobs The Almanack of Ravikant and Hooked Nothing excites me more than exchanging opinions through productive conversations youtube com Got any doubt or wanna chat React out to me on twitter or linkedin Until next time happy coding |
2023-08-14 18:05:24 |
海外TECH |
DEV Community |
Python Developer's Guide to Character Encoding |
https://dev.to/honeybadger/python-developers-guide-to-character-encoding-2e8i
|
Python Developer x s Guide to Character EncodingThis article was originally written by Anita Achu on the Honeybadger Developer Blog Character encoding is a common problem in software development Like other programming languages character encoding in Python can be troublesome In this article we will dive deep into character encoding discuss ways to interact with text and bytes in your Python project and fix common encoding errors using character encoding in Python What is Character Encoding In human language text files on a computer contain a bunch of characters made of text or sentences which could include English text a or Latin text ā In computer language however this text file contains bits and bytes not text Like English or Latin computer stores characters as bytes These bytes are more like computer codes which are translated into human readable text This translation is character encoding Character encoding is a set of methods for mapping raw binary to readable characters text using an encoding lookup table Every character is assigned a unique ID number which helps computers read and understand text Multiple types of character encodings are used for interpreting bytes Often the wrong character encoding is applied when interpreting bytes causing them to display as strange looking characters such as voil├Ā‡å ã or an unknown character such as ������ even worse it could cause an error that crashes your program While working with characters in Python you will encounter two main data types strings and bytes Character encoding revolves around encoding and decoding these data types String ModuleStrings are computer bytes interpreted and displayed in human readable form Before the development of Python strings used the binary format by default to store bytes It implies that strings are bytes with ASCII set as the default encoding While ASCII has been quite useful it can only encode English language characters Solving this problem led to the development of Universal Standard Encoding Unicode Unicode is a universal character set that defines all characters for different human languages used on computers with over one million code points These code points refer to displayed text or characters In Python every string uses the Unicode format to represent characters by default It implies that each text has a specific code point that displays the characters using UTF as the default encoding BytesThe second data type is bytes which are a series of integers A byte is a collection of eight bits that represent a unit of information Characters in a computer include text or strings made up of one or more bytes Imagine that you receive a file containing data in bytes You will need to translate these bytes into readable text In this case character encoding converts the character you select into the correct bytes in the computer s memory before reading the bytes back into characters to show the text Working with Character Encoding in PythonAs stated previously Python has two data types strings and bytes The process of moving between strings and bytes is known as encoding and decoding in Python Now let us dive into how this works Encoding Strings into BytesThe process of converting strings text to computer bytes is known as encoding In Python strings represent human readable text which is Unicode characters Unicode is not encoding but an abstract encoding standard that uses Unicode Transformation Format UTF for encoding Although there are multiple character encodings we will be working with UTF which is the default encoding for Python UTF refers to the standard Python encoding and refers to the bit units used in character encoding UTF is a standard and efficient encoding of Unicode strings that represents characters in one two three or four byte units Python uses UTF by default which means it does not need to be specified in every Python file To encode a string into bytes add the encode method which will return the binary representation of the string gt gt gt text Hello World gt gt gt text encode utf b Hello Word The output is the binary representation of Hello Next let s look at something more complex gt gt gt text parlé gt gt gt text encode utf b parl xc xa gt gt gt text résumé gt gt gt text text encode utf b r xc xasum xc xa In the code above the letters parl are ASCII characters which allows them to be represented Each character in the ASCII table represents a single byte However complicated characters such as é are not ASCII compatible in UTF and are represented by two bytes encoded xc and xa as in the first example In the second example strings that are not ASCII compatible are represented by three bytes encoded UTF can go up to four bytes encoded It means complicated characters in UTF require several bytes for their binary representation Decoding Bytes into StringsConverting a bytes object to a string is known as decoding To decode bytes into strings call the decode method and specify the type of character encoding you wish to use Of course we are using UTF gt gt gt text b parl xc xa gt gt gt text decode utf parlé gt gt gt text b r xc xasum xc xa gt gt gt text decode utf résumé When we pass a binary format along with the decode method the output is our original string Remember you do not have to specify UTF when working with Python We are only specifying it here to show the encoding used Reading a Text File in PythonA file on a computer does not contain readable text To read the characters in the file as text you need to use the read method In addition in Python when you open a file for reading or writing it is best practice to state the character encoding with which you are working This is because when working with text files Python uses different character encodings depending on the operating system by default Usually when you open a file using the open method Python automatically treats it as a text file to convert the bytes in the text file to a string with the encoding you want It is best to specify the encoding Here is an example gt gt gt with open data txt mode r encoding utf as f message f read Writing a Text File in PythonYou can also use the open method to write files To write set it to write by entering mode w gt gt gt with open data txt mode w encoding utf as f f write Hi I am having fun learning Python Other Encodings Available in PythonAs mentioned previously there are multiple encodings available for Unicode characters in Python We already discussed UTF which is the most common and widely used It is also the default encoding for Python but there are others UTF UTF encoding for Unicode characters represents characters in two or four bytes The lowest binary representation of a character in UTF consists of two bytes The major advantage UTF has over UTF is that while the former uses one byte for encoding an ASCII character the latter encodes the same character with two or more bytes A UTF encoded English text file is at least twice as large as a UTF encoded version of the same file UTF UTF uses fixed four bytes for encoding Unicode characters This means that every character encoding uses four bytes UTF uses more memory compared to UTF and UTF It is faster and preferable for string manipulation because you can calculate the length of a string in bytes using the number of characters in the string However for every ASCII character you use an additional three bytes Pitfalls and How to Fix ThemAvoid character encoding errors at all cost as they are troublesome and no developer enjoys spending time dealing with a bug Let us look at common pitfalls in character encoding and how to fix likely errors One common error raised when working with character encoding in Python is the UnicodeEncodeError There are a few causes of this error First a UnicodeEncodeError error may occur when using characters that cannot be encoded such as emojis Unicode supports the vast majority of languages but not all Therefore a character not supported in Unicode will fail Second when the strict method is used by default for encoding and decoding it will cause an error if a character cannot be encoded or decoded There is also a UnicodeDecodeError which occurs when the character encoding of the bytes we are reading and the character encoding Python is attempting to use to read them are not similar How Can I Avoid or Fix This Error One way to fix a character encoding error is to use the ignore or replace method to remove special characters or emojis that cannot be encoded You can also use the ignore method when opening a file to avoid any errors Here is an example text ф with open message txt w encoding utf errors ignore as f f write text ConclusionComputers do not recognize text they store data in binary format Character encoding is the key that converts this binary data to readable text In this article we have discussed several topics The early method of character encoding ASCII was insufficient as it did not allow non English characters to be represented in binary format This was resolved by the introduction of Unicode which assigned a specific code point for every human readable character We also discussed how encoding works in Python and the various character encoding methods in Python I hope this tutorial was helpful to you Happy Coding |
2023-08-14 18:04:25 |
Apple |
AppleInsider - Frontpage News |
Netflix starts testing games service for smart TVs |
https://appleinsider.com/articles/23/08/14/netflix-starts-testing-games-service-for-smart-tvs?utm_medium=rss
|
Netflix starts testing games service for smart TVsNetflix has confirmed it will be bringing its gaming service to set top boxes and smart televisions but it probably won t arrive on the Apple TV just yet Netflix An app that acted as a game controller for Netflix games was discovered on August sparking rumors of an introduction of its games platform to televisions In a Monday announcement Netflix confirmed the rumors were true Read more |
2023-08-14 18:29:18 |
Apple |
AppleInsider - Frontpage News |
Take home the latest MacBook Air at a $150 discount, plus big savings on iPads & AirPods |
https://appleinsider.com/articles/23/08/14/take-home-the-latest-macbook-air-at-a-150-discount-plus-big-savings-on-ipads-airpods?utm_medium=rss
|
Take home the latest MacBook Air at a discount plus big savings on iPads amp AirPodsFor most students summer is coming to a close but that doesn t mean the deep discounts are over too Amazon continues slashing prices on gear across Apple s lineup including a discount on the latest inch MacBook Air Apple s deep savings for the week of August th Amazon also dropped prices on several iPad models some upwards of off retail price Plus take home all the accessories you need like the Apple Pencil or Magic Keyboard case both with significant savings Read more |
2023-08-14 18:13:50 |
海外TECH |
Engadget |
Telegram Stories are no longer limited to paid users |
https://www.engadget.com/telegram-stories-are-no-longer-limited-to-paid-users-180415572.html?src=rss
|
Telegram Stories are no longer limited to paid usersTelegram launched its Stories feature to everyone today following its availability to Premium users starting last month Like Facebook Messenger s Stories they appear as expandable bubbles above your conversation However Telegram s take is more customizable providing granular control of who sees uploaded posts and for how long “Now when you meet people on Telegram you ll see exciting snapshots of their life ーnot just a few profile photos the company wrote in a blog post today Launching alongside the messaging service s th birthday Telegram describes Stories as “by far the most requested feature in the company s decade long history Its privacy controls include visibility options for everyone all contacts selected contacts or close friends TelegramTelegram s Stories also let you hide your posts from contacts you don t want to see and Premium users can choose between six and hours of visibility for new posts In addition post creators can see a list of the Telegram users who viewed their content It also supports a BeReal like dual camera mode letting you simultaneously share photos or videos captured by your phone s front and rear sensors The feature also includes reactions so viewers can add a heart or choose from “hundreds of other responses to posts Some of Stories more advanced controls are reserved for Premium subscribers per month Perhaps most significant paying users posts display first giving them more exposure Subscribers can also view others stories in stealth mode hiding all traces of their visit from the author Additionally subscribers get the previously mentioned custom expiration options a permanent view history see who viewed your posts even after they expire the ability to save Stories to the gallery “ times longer captions and a higher allotment of daily Stories up to Telegram Stories is scheduled to roll out to the service s iOS and Android apps today This article originally appeared on Engadget at |
2023-08-14 18:04:15 |
海外TECH |
CodeProject Latest Articles |
Async Lazy In C# – With Great Power Comes Great Responsibility |
https://www.codeproject.com/Articles/5366567/Async-Lazy-In-Csharp-With-Great-Power-Comes-Great
|
initialization |
2023-08-14 18:35:00 |
ニュース |
BBC News - Home |
PSNI data breach: Details of NI police in hands of dissident republicans |
https://www.bbc.co.uk/news/uk-northern-ireland-66479818?at_medium=RSS&at_campaign=KARANGA
|
officers |
2023-08-14 18:11:39 |
ニュース |
BBC News - Home |
Moises Caicedo transfer news: Chelsea sign Brighton midfielder for £100m |
https://www.bbc.co.uk/sport/football/66504891?at_medium=RSS&at_campaign=KARANGA
|
british |
2023-08-14 18:53:11 |
ニュース |
BBC News - Home |
The Hundred 2023: Watch Joe Root's ramp six caught by groundskeeper |
https://www.bbc.co.uk/sport/av/cricket/66502546?at_medium=RSS&at_campaign=KARANGA
|
The Hundred Watch Joe Root x s ramp six caught by groundskeeperWatch a Sophia Gardens groundskeeper take a superb catch off a brilliant six from Trent Rockets and England batter Joe Root in The Hundred |
2023-08-14 18:25:33 |
コメント
コメントを投稿